2015-04-03 37 views
11

我有一些带有一些字形的图形,但只想显示某些字形的工具提示。目前有什么方法可以在Bokeh中完成此操作?散景绘图:仅为某些字形启用工具提示

或者,有没有办法将两个数字打在一起?看起来这样可以让我完成我想要做的事情。

回答

11

由于这个页面在谷歌论坛我想出如何可以做到这一点。 Link here

编辑2015年10月20日:看起来像谷歌组链接已经不遗憾的是工作。这是来自Sarah Bird @bokehplot的消息。

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

从本质上讲,你需要(背景虚化版本0.9.2):当你创建人物

  • 创建字形单独
  • 添加的字形你的身材

    1. 不是在你的tools添加hover
    2. 为这组字形设置悬停工具
    3. 将悬停工具添加到您的数字

    例子:

    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) 
    
  • +0

    只需要注意,如果你这样做,工具提示将不尊重对方的空间,如果你碰巧在同一时间悬停在两个不同的字形上,工具提示将互相重叠。他们尊重彼此的空间吗? – Guillochon 2016-03-26 18:40:27

    +0

    是的,我也注意到了这一点。明看这个问题,但很想听听somone是否有解决方案。 – WillZ 2016-03-26 21:40:41

    1

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

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

    +0

    呀,我想到了这一点,但只是没有办法做到这一点,除非我想一个情节保存为一个数字,然后在其上绘制,这是真的很hacky – Imaduck 2015-04-06 15:55:25

    +0

    @Imaduck这将是一个伟大的功能,我想在散景中,我试图做同样的事情,但工具提示弹出我不需要的那些字形(但仍然想要在图表中看到).. – WillZ 2015-08-20 10:05:07

    7

    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(除非需要不同的工具提示),只需将其添加到现有工具的渲染器即可。
    +0

    只是惊人的! – denvar 2016-06-25 20:03:02

    +0

    是的,这是一个问题,我最近才注意到它。 github已经存在一个问题,可能会将这些悬停工具分组。 https://github.com/bokeh/bokeh/issues/5497 – WillZ 2017-01-18 22:09:48

    +1

    @WillZ我认为目前的实施是可以的,只是无意中创造了大量工具。我可以想象出一些情况,即同一个地块上的两个字形会受益于不同的工具提示。 – tterry 2017-01-19 20:34:03

    0

    你需要对你有兴趣在具有悬停工具活跃了,然后设置在悬停工具的names=属性名称中的字形的name=属性来命名字形。 (注意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') 
    
    相关问题