Wil*_*llZ 15
感谢Google网上论坛的这个页面,我想出了如何做到这一点. 链接在这里
编辑2015-10-20:遗憾的是,谷歌群组链接看起来不再有用了.这是来自Sarah Bird @bokehplot的消息.
编辑2017-01-18:目前,这会在工具栏中添加多个悬停工具图标.这可能会导致问题.github已经在这里提出了一个问题.或者,在下面的答案中尝试@ tterry的解决方案.
基本上你需要(散景版0.9.2):
hover在tools创建图形时不要添加例:
import bokeh.models as bkm
import bokeh.plotting as bkp
source = bkm.ColumnDataSource(data=your_frame)
p = bkp.figure(tools='add the tools you want here, but no hover!')
g1 = bkm.Cross(x='col1', y='col2')
g1_r = p.add_glyph(source_or_glyph=source, glyph=g1)
g1_hover = bkm.HoverTool(renderers=[g1_r],
tooltips=[('x', '@col1'), ('y', '@col2')])
p.add_tools(g1_hover)
# now repeat the above for the next sets of glyphs you want to add.
# for those you don't want tooltips to show when hovering over, just don't
# add hover tool for them!
Run Code Online (Sandbox Code Playgroud)
此外,如果您需要为要添加的每个字形添加图例,请尝试使用bokeh.plotting_helpers._update_legend()方法.github来源例如:
_update_legend(plot=p, legend_name='data1', glyph_renderer=g1_r)
Run Code Online (Sandbox Code Playgroud)
tte*_*rry 10
张的回答是否有效,但最终会有多个悬停工具.如果这是不合需要的,您可以将渲染器添加到现有的悬停工具:
from bokeh import plotting
from bokeh.models import HoverTool, PanTool, ResetTool, WheelZoomTool
hover_tool = HoverTool(tooltips=[('col', '@x'),('row', '@y')]) # instantiate HoverTool without its renderers
tools = [hover_tool, WheelZoomTool(), PanTool(), ResetTool()] # collect the tools in a list: you can still update hover_tool
plot = plotting.figure(tools=tools)
plot.line(x_range, y_range) # we don't want to put tooltips on the line because they can behave a little strange
scatter = plot.scatter(x_range, y_range) # we assign this renderer to a name...
hover_tool.renderers.append(scatter) # ...so we can add it to hover_tool's renderers.
Run Code Online (Sandbox Code Playgroud)
所以这里的差异:
plotting界面以高级方式创建您的字形,这仍然有效. 小智 9
您需要使用希望name=激活悬停工具的字形上的属性来命名该字形,然后在悬停工具的names=属性中设置该名称。(请注意以下示例中字形的name=属性fig.line。
hover = HoverTool( mode='vline', line_policy='nearest', names=['ytd_ave'],
tooltips=[
("Week Number", "@WeekNumber"),
("OH for the Week", "@OverHead{0.00}%"),
("OH Average", "@AveOverHead{0.00}%"),
("Non-Controllable Hours", "@NonControllableHours{0.0}"),
("Controllable Hours", "@ControllableHours{0.0}"),
("Total Hours", "@TotalHours{0.0}"),
]
)
fig = Figure(title='Weekly Overhead', plot_width=950, plot_height=400,
x_minor_ticks=2, tools=['pan', 'box_zoom', 'wheel_zoom', 'save',
'reset', hover])
ch = fig.vbar('WeekNumber', top='ControllableHours', name='Over Head',
color='LightCoral', source=sources, width=.5)
nch = fig.vbar('WeekNumber', bottom='ControllableHours', top='TotalOHHours',
name='Non-Controllable Over Head', color='LightGray',
source=sources, width=.5)
bh = fig.vbar('WeekNumber', bottom='TotalOHHours', top='TotalHours',
name='Project Hours', color='LightGreen', source=sources,
width=.5)
ave = fig.line('WeekNumber', 'AveOverHead', source=sources, color='red',
y_range_name='Percent_OH', name='ytd_ave')
Run Code Online (Sandbox Code Playgroud)