【发布时间】:2015-06-08 17:18:48
【问题描述】:
我有一个带有一些字形的图形,但只想显示某些字形的工具提示。目前有没有办法在 Bokeh 中实现这一点?
或者,有没有办法将两个图形相互叠加?看来这会让我完成我想做的事情。
【问题讨论】:
标签: python plot hover tooltip bokeh
我有一个带有一些字形的图形,但只想显示某些字形的工具提示。目前有没有办法在 Bokeh 中实现这一点?
或者,有没有办法将两个图形相互叠加?看来这会让我完成我想做的事情。
【问题讨论】:
标签: python plot hover tooltip bokeh
感谢 Google Groups 中的这个页面,我想出了如何做到这一点。 Link here
编辑 2015-10-20:不幸的是,谷歌群组链接似乎不再有效。这是来自 Sarah Bird @bokehplot 的消息。
编辑 2017-01-18:目前这会在工具栏中添加多个悬停工具图标。这可能会导致问题。已经在 github here 提交了一个问题。或者,在下面的答案中尝试@terry 的解决方案。
基本上你需要(散景版本 0.9.2):
tools 中添加hover
例子:
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!
此外,如果您需要为要添加的每个字形添加图例,请尝试使用bokeh.plotting_helpers._update_legend() 方法。 github source 如:
_update_legend(plot=p, legend_name='data1', glyph_renderer=g1_r)
【讨论】:
您需要使用您有兴趣激活悬停工具的字形上的name= 属性来命名您的字形,然后在悬停工具的names= 属性中设置该名称。 (请注意以下示例中 fig.line 字形的 name= 属性。
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')
【讨论】:
'hover' 添加到tools 列表参数中,而不是显式创建HoverTool,然后:hover_tool = fig.select(type=HoverTool) hover_tool.names = ["ytd_ave"]
Will Zhang's answer 可以工作,但您最终会得到多个悬停工具。如果不希望这样做,您可以将渲染器添加到现有的悬停工具:
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.
所以这里的区别:
plotting 接口以高级方式创建字形,这仍然有效。 【讨论】:
来自维护者的更新:现在行和图像都支持悬停
已过时:
图像类型字形和线条字形目前不支持悬停。因此,将这些字形之一与支持悬停工具提示的字形结合使用可能是一种解决方法。
见: http://docs.bokeh.org/en/latest/docs/user_guide/objects.html#hovertool
【讨论】: