6

I have more than one line on a bokeh plot, and I want the HoverTool to show the value for each line, but using the method from a previous stackoverflow answer isn't working:

https://stackoverflow.com/a/27549243/3087409

Here's the relevant code snippet from that answer:

fig = bp.figure(tools="reset,hover")
s1 = fig.scatter(x=x,y=y1,color='#0000ff',size=10,legend='sine')
s1.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}
s2 = fig.scatter(x=x,y=y2,color='#ff0000',size=10,legend='cosine')
fig.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}

And here's my code:

from bokeh.models import HoverTool
from bokeh.plotting import figure

source = ColumnDataSource(data=dict(
    x = [list of datetimes]
    wind = [some list]
    coal = [some other list]
    )
)

hover = HoverTool(mode = "vline")

plot = figure(tools=[hover], toolbar_location=None,
    x_axis_type='datetime')

plot.line('x', 'wind')
plot.select(dict(type=HoverTool)).tooltips = {"y":"@wind"}
plot.line('x', 'coal')
plot.select(dict(type=HoverTool)).tooltips = {"y":"@coal"}

As far as I can tell, it's equivalent to the code in the answer I linked to, but when I hover over the figure, both hover tools boxes show the same value, that of the wind.

thosphor
  • 2,493
  • 7
  • 26
  • 42

1 Answers1

16

You need to add renderers for each plot. Check this. Also do not use samey label for both values change the names.

from bokeh.models import HoverTool
from bokeh.plotting import figure

source = ColumnDataSource(data=df)

plot = figure(x_axis_type='datetime',plot_width=800, plot_height=300)

plot1 =plot.line(x='x',y= 'wind',source=source,color='blue')
plot.add_tools(HoverTool(renderers=[plot1], tooltips=[('wind',"@wind")],mode='vline'))

plot2 = plot.line(x='x',y= 'coal',source=source,color='red')
plot.add_tools(HoverTool(renderers=[plot2], tooltips=[("coal","@coal")],mode='vline'))

show(plot)

The output look like this. OUTPUT

Space Impact
  • 13,085
  • 23
  • 48
  • Thanks, this worked. It also ties in with another answer on the page I linked: https://stackoverflow.com/a/34534995/3087409 – thosphor Mar 16 '18 at 10:28
  • 1
    @user3087409 Yes,The underlying concept is same, whereas the solution is specific to this problem. Also you said it didn't worked for you. So changed code to your scenario and posted. – Space Impact Mar 16 '18 at 11:59