Bokeh Plotting: Enable tooltips for only some glyphs
Asked Answered
K

4

35

I have a figure with some glyphs, but only want tooltips to display for certain glyphs. Is there currently a way to accomplish this in Bokeh?

Alternatively, is there a way to plot two figures on top of each other? It seems like that would let me accomplish what I want to do.

Kulak answered 3/4, 2015 at 15:35 Comment(0)
J
30

Thanks to this page in Google Groups I figured out how this can be done. Link here

Edit 2015-10-20: looks like the google group link doesn't work anymore unfortunately. It was a message from Sarah Bird @bokehplot.

Edit 2017-01-18: Currently this would add multiple hover tool icons to the tool bar. This may cause problems. There is already an issue filed at github here. Alternatively, try @tterry's solution in the answer below.

Essentially you need to (bokeh version 0.9.2):

  1. not add hover in your tools when you create the figure
  2. create glyphs individually
  3. add glyphs to your figure
  4. set up the hover tool for this set of glyphs
  5. add the hover tool to your figure

Example:

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!

Also if you need to add legend to each of the glyphs you are adding, try using bokeh.plotting_helpers._update_legend() method. github source Eg:

_update_legend(plot=p, legend_name='data1', glyph_renderer=g1_r)
Jobe answered 20/8, 2015 at 11:25 Comment(3)
Just a note if you do this that the tooltips will not respect each others space, the tooltips will overlap one another if you happen to hover over two different glyphs at the same time. Any way to get them to respect each others space?Calgary
Yes I noticed this too. TBH I haven't spent timing looking at this issue, but would love to hear if somone has a solution.Jobe
Here is a link to a git issue with a description of how to make hover tools work only for a subset of glyphs. It includes someone named birdsarah, so maybe this is a new link to replace the one that went dead on you: github.com/bokeh/bokeh/issues/2076Bonne
K
20

You need to name your glyph with the name= attribute on the glyph that you are interested in having the hover tool active for and then set that name in the hover tool's names= attribute. (Note the name= attribute of the fig.line glyph in the example below.

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')
Kuhns answered 3/5, 2017 at 12:8 Comment(1)
Instead of explicitly creating the HoverTool, you can add 'hover' to tools list argument when creating the figure and then: hover_tool = fig.select(type=HoverTool) hover_tool.names = ["ytd_ave"]Preschool
T
12

Will Zhang's answer will work, but you would end up with multiple hover tools. If this is undesirable, you can add renderers to an existing hover tool:

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.

So the differences here:

  1. You can create your glyph in a high level way using the plotting interface and this will still work.
  2. You don't have to create a new HoverTool (unless you want different tooltips) each time, just add it to the existing tool's renderers.
Tights answered 1/6, 2016 at 2:30 Comment(3)
Yes, this is an issue, I only noticed it recently. There is an issue filed in github already to potentially group these hover tools. github.com/bokeh/bokeh/issues/5497Jobe
@Jobe I think the current implementation is OK, just has a gotcha around accidentally creating a ton of tools. I can imagine some cases where two glyphs on the same plot would benefit from different tooltips.Tights
@Tights I agree. Just a convenience thingJobe
S
2

UPDATE from maintainters: hover IS now supported on both lines and images



OBSOLETE:

Hover is not currently supported for image type glyphs and line glyphs. So, using one of these glyphs in combination with glyphs that support hover tool tip, might be a work around.

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

Simonides answered 6/4, 2015 at 5:29 Comment(2)
Yeah, I thought about this, but there's just no way to do it unless I want to save a plot as a figure then plot over it, which is really hacky.Kulak
@Kulak This will be a great feature to have in Bokeh I think. I'm trying to do the same thing, but tool tips are popping up for those glyphs that I don't need (but still want to see in the chart)..Jobe

© 2022 - 2024 — McMap. All rights reserved.