【问题标题】:Bokeh Plotting: Enable tooltips for only some glyphs散景绘图:仅对某些字形启用工具提示
【发布时间】:2015-06-08 17:18:48
【问题描述】:

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

或者,有没有办法将两个图形相互叠加?看来这会让我完成我想做的事情。

【问题讨论】:

    标签: python plot hover tooltip bokeh


    【解决方案1】:

    感谢 Google Groups 中的这个页面,我想出了如何做到这一点。 Link here

    编辑 2015-10-20:不幸的是,谷歌群组链接似乎不再有效。这是来自 Sarah Bird @bokehplot 的消息。

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

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

    1. 创建图窗时不要在tools 中添加hover
    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!
    

    此外,如果您需要为要添加的每个字形添加图例,请尝试使用bokeh.plotting_helpers._update_legend() 方法。 github source 如:

    _update_legend(plot=p, legend_name='data1', glyph_renderer=g1_r)
    

    【讨论】:

    • 请注意,如果您这样做,工具提示将不尊重彼此的空间,如果您碰巧同时将鼠标悬停在两个不同的字形上,工具提示将相互重叠。有什么办法让他们尊重彼此的空间?
    • 是的,我也注意到了这一点。 TBH 我没有花时间研究这个问题,但很想听听某人是否有解决方案。
    • 这里是一个 git 问题的链接,其中描述了如何使悬停工具仅适用于字形的子集。它包括一个名叫birdsarah的人,所以也许这是一个新链接来替换你死去的那个:github.com/bokeh/bokeh/issues/2076
    【解决方案2】:

    您需要使用您有兴趣激活悬停工具的字形上的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"]
    【解决方案3】:

    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.
    

    所以这里的区别:

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

    【讨论】:

    • 是的,这是个问题,我最近才注意到。已经在 github 中提交了一个问题,以可能对这些悬停工具进行分组。 github.com/bokeh/bokeh/issues/5497
    • @WillZ 我认为当前的实现还可以,只是在意外创建大量工具时遇到了问题。我可以想象在某些情况下,同一图上的两个字形会受益于不同的工具提示。
    • @terry 我同意。只是为了方便
    【解决方案4】:

    来自维护者的更新:现在行和图像都支持悬停



    已过时:

    图像类型字形和线条字形目前不支持悬停。因此,将这些字形之一与支持悬停工具提示的字形结合使用可能是一种解决方法。

    见: http://docs.bokeh.org/en/latest/docs/user_guide/objects.html#hovertool

    【讨论】:

    • 是的,我想过这个,但是除非我想将绘图保存为图形然后在它上面绘图,否则没有办法做到这一点,这真的很hacky。
    • @Imaduck 我认为这将是 Bokeh 中的一个很棒的功能。我正在尝试做同样的事情,但是对于那些我不需要的字形(但仍希望在图表中看到)弹出工具提示..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多