Hover issues when using a single CDS

When the same CDS is used for plotting multiple glyphs, hovering over the last added glyph highlights all of them. In the example below, hovering over l1 and l2 does not highlight any of them, but hovering over l3 (the last one added with hover) highlights all of them. Is this a bug or expected behavior?

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

source = ColumnDataSource(data={
    'x': [1, 2, 3],
    'y1': [1, 2, 3],
    'y2': [2, 3, 4],
    'y3': [3, 4, 5],
})

p = figure(tools='hover')

l1 = p.line(x='x', y='y1', source=source, line_color='red', line_width=1, hover_line_width=2)
l2 = p.line(x='x', y='y2', source=source, line_color='green', line_width=1, hover_line_width=2)
l3 = p.line(x='x', y='y3', source=source, line_color='blue', line_width=1, hover_line_width=2)

show(p)

Even if the renderer is explicitly set:

from bokeh.models import HoverTool
tool = HoverTool(renderers=[l1])
p.add_tools(tool)

hovering over l1 highlights all the glyphs.

Yes. This is intended behaviour. See Providing data — Bokeh 2.4.0 Documentation
. The idea behind it to show linkages between records (i.e. indices) in a ColumnDataSource. I use this to link spatial and transient data (e.g. hover over a plan view map, a corresponding line showing water level over time in a totally different plot highlights too, showing you “ok at the this location water level changed like this over time”.

In the background, mechanically what’s happening when you hover over a particular glyph item is that the “inspected.indices” property of the underlying CDS driving the glyph is being updated/changed. Whether that inspected.indices property update is “triggerable” is dependent on the renderer(s) you assign to the HoverTool. This and the “selected.indices” property work the same way but for selecting glyph items as opposed to hovering/inspecting.

Thanks @gmerritt123 . I understand the idea behind it that hovered index is highlighted in all the glyphs using the same CDS. This works fine in the case of scatter plots, but if there is a line glyph it just highlights all the glyphs using that CDS, i.e. all the indices, which makes it less usable.

There is still the issue though that hovering only works on the last added glyph (l3 in the example), and not on the other ones (l1 and l2).

This works fine in the case of scatter plots, but if there is a line glyph it just highlights all the glyphs using that CDS, i.e. all the indices, which makes it less usable.

I just resort to a lot of CDS’s in that case, not ideal but a fair trade off given the power of the out of the box linkage. An alternative would be, for the renderers you don’t want to trigger hover on, change their renderer’s hover_glyph property to be = to their glyph property (basically so when they get hovered over, nothing will visually change). Try playing around with that and it might accomplish what you need.

There is still the issue though that hovering only works on the last added glyph (l3 in the example), and not on the other ones (l1 and l2).

Yeah that is odd and I think I have encountered similar weirdness with the multiline glyph → see Multiline hover_glyph "half" failing [BUG] · Issue #11853 · bokeh/bokeh · GitHub , maybe that rabbit hole can shed a bit of light on the situation.

It indeed seems to be a known bug. I also read a bit through the GitHub issues and discussions following your link and the docs and the highlighting behavior currently is expected behavior. I hope it gets somehow resolved soon.

It the meantime I’ll try to find a workaround. I prefer not to create multiple CDSs - it may be fine for simple cases, but in my case it would just increase code complexity, it’s more error prone, and may affect plot updating performance.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.