散景图:仅为某些字形启用工具提示

Ima*_*uck 20 python plot tooltip hover bokeh

我有一些带有一些字形的图形,但只想要显示某些字形的工具提示.目前有没有办法在Bokeh中实现这一目标?

或者,有没有办法将两个数字叠加在一起?似乎这会让我完成我想做的事情.

Wil*_*llZ 15

感谢Google网上论坛的这个页面,我想出了如何做到这一点. 链接在这里

编辑2015-10-20:遗憾的是,谷歌群组链接看起来不再有用了.这是来自Sarah Bird @bokehplot的消息.

编辑2017-01-18:目前,这会在工具栏中添加多个悬停工具图标.这可能会导致问题.github已经在这里提出了一个问题.或者,在下面的答案中尝试@ tterry的解决方案.

基本上你需要(散景版0.9.2):

  1. hovertools创建图形时不要添加
  2. 单独创建字形
  3. 为你的身材添加字形
  4. 为这组字形设置悬停工具
  5. 将悬停工具添加到您的图中

例:

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)

所以这里的差异:

  1. 您可以使用plotting界面以高级方式创建您的字形,这仍然有效.
  2. 您不必每次都创建一个新的HoverTool(除非您需要不同的工具提示),只需将其添加到现有工具的渲染器即可.

  • @WillZ我认为当前的实现还可以,只是偶然地创建了很多工具而已。我可以想象在某些情况下同一情节上的两个字形将从不同的工具提示中受益。 (2认同)

小智 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)