Python - Bokeh vbar悬停工具

时间:2018-04-08 21:06:25

标签: python bokeh

我有一个简单的多数据条形图(非堆叠),希望能够在鼠标悬停时显示条形图的(最大)值。

我无法将悬停位置与数据相关联。我不确定如何从条形图调用索引的语法/编码。

这是我的代码:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

output_file("bars.html")

LOCATIONS = ['CPC','OG2','HS82-83','IG6','IG4','IG10']
CHECKS = ['AID CHECKS', 'ITEMS SCREENED', 'PERSONS SCREENED']

data = {'LOCATIONS' : LOCATIONS,
        'AID CHECKS'   : [208,622,140,1842,127,1304],
        'PERSONS SCREENED'   : [201,484,126,1073,81,676],
        'ITEMS SCREENED' : [28,71,31,394,32,207]}



x = [ (location, check) for location in LOCATIONS for check in CHECKS ]
counts = sum(zip(data['AID CHECKS'], data['PERSONS SCREENED'], data['ITEMS SCREENED']), ()) # like an hstack

source = ColumnDataSource(data=dict(x=x, counts=counts))



p = figure(x_range=FactorRange(*x), plot_height=600, plot_width=990, title="NPS Locations by Security Checks",
           tools="pan,wheel_zoom,box_zoom,reset, save")

p.xaxis.axis_label_text_font_size = "5pt"
p.xaxis.axis_label_text_font_style='bold'

p.vbar(x='x', top='counts', width=0.9, source=source)

p.add_tools(HoverTool(tooltips=[("LOCATION", "@location"), ("TOTAL", "@check")]))


p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None

show(p)

1 个答案:

答案 0 :(得分:2)

调整以下行:

p.add_tools(HoverTool(tooltips=[("LOCATION", "@x"), ("TOTAL", "@counts")]))

请参阅documentation

  

以@开头的字段名称与a中的列相关联   ColumnDataSource。例如,将显示字段名称“@price”   每当触发悬停时,“价格”列中的值。如果   悬停是第17个字形,然后是悬停工具提示   相应地显示第17个价格值。