ragesz ragesz - 1 month ago 5x
Python Question

Python bokeh apply hovertools only on model not on figure

I want to have a scatter plot and a (base)line on the same figure. And I want to use

only on the circles of scatter but not on the line. Is it possible?

With the code below I get tooltips with
index: 0
(x, y): (???, ???)
when I hover on the line (any part of the line). But the
index: 0
data in
is totally different (
(x, y): (1, 2)

df = pd.DataFrame({'a':[1, 3, 6, 9], 'b':[2, 3, 5, 8]})

from bokeh.models import HoverTool
import bokeh.plotting as bplt

TOOLS = ['box_zoom', 'box_select', 'wheel_zoom', 'reset', 'pan', 'resize', 'save']

source = bplt.ColumnDataSource(data=df)
hover = HoverTool(tooltips=[("index", "$index"), ("(x, y)", "(@a, @b)")])

p = bplt.figure(plot_width=600, plot_height=600, tools=TOOLS+[hover],
title="My sample bokeh plot", webgl=True)

p.circle('a', 'b', size=10, source=source)

p.line([0, 10], [0, 10], color='red')

bplt.save(p, 'c:/_teszt.html')

Thank you!!


To limit which renderers you want the HoverTool is active on (by default it's active on all) you can either set a name attr on your glyphs, then specify which names you want your HoverTool to be active on:

p.circle('a', 'b', size=10, name='circle', source=source)
hover = HoverTool(names=['circle'])

docs: http://bokeh.pydata.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool.names

or you can add the renderers to the HoverTool.

circle = p.circle('a', 'b', size=10, source=source)
hover = HoverTool(renderers=['circle'])

docs: http://bokeh.pydata.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool.renderers