【问题标题】:Correctly showing Hover Tooltips on Bokeh Wedge Chart在散景楔形图上正确显示悬停工具提示
【发布时间】:2018-01-17 15:50:44
【问题描述】:

我正在尝试将悬停工具顶部添加到使用楔形字形制作的散景饼图中,但悬停工具显示给定楔形的多个值。有没有办法纠正这个问题?代码是:

import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_file
from bokeh.models import HoverTool, ColumnDataSource
from math import pi



percents = [0, 5/143, 51/143, 88/143, 108/143, 141/143, 1.0]
category = ['A ', 'B ', 'C ', 'D ', 'E ', 'F']
counts = [5, 46, 37, 20, 33, 2]
starts = [1/2*pi-(p*2*pi) for p in percents[:-1]]
ends = [1/2*pi-(p*2*pi) for p in percents[1:]]
colors = ['#889dba', '#1f356f', '#1e92b8', '#33748a', '#a5d3e3', '#bbc2d4']
# create source
source = ColumnDataSource(
    data=dict(
        x=[0 for x in percents],
        y=[0 for x in percents],
        radius = [0.5 for x in percents],
        percents=percents,
        category= category,
        starts=starts,
        colors=colors,
        ends=ends,
        counts = counts
    )
)

TOOLS = "hover"

p = figure(plot_width = 500, plot_height = 500, x_axis_label = None, y_axis_label = None,
title = 'Type', tools = TOOLS)

p.title.align = 'center'
p.title.text_font = 'arial narrow'

p.wedge(x='x', y='y',  radius = 'radius', direction="clock",
                start_angle='starts', end_angle='ends', color='colors', source=source)

hover = p.select(dict(type=HoverTool))
hover.tooltips = [
    ('category', '@category'),
    ('percents','@counts')
]


p.axis.visible = False
p.ygrid.visible = False
p.xgrid.visible = False

output_file(pie.html')
show(p)

这是图片在悬停时的样子:

【问题讨论】:

    标签: python data-visualization bokeh


    【解决方案1】:

    不久前我遇到了类似的问题。老实说,我不知道这是一个错误还是我们真的做错了什么。

    您可以通过反转百分比列表(和标签)来临时解决它:

    percents = percents[::-1]
    category = category[::-1]
    counts = counts[::-1]
    

    然后从楔形中删除顺时针语句:

    direction="clock",
    

    【讨论】:

    • 看起来可能很大,请向 MRE 提交 GH 问题
    • 有人已经将其报告为错误;它已关闭,他们建议使用 @filippo 的方法,该方法对我有用。
    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多