在Bokeh Wedge图表上正确显示悬停工具提示

时间:2018-01-17 15:50:44

标签: python data-visualization bokeh

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

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)

这就是图片在悬停时的外观: bokeh_pie

1 个答案:

答案 0 :(得分:2)

前一段时间我遇到过类似的问题。老实说,我不知道这是一个错误还是实际上我们做错了什么。

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

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

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

direction="clock",