Multiple HoverTools for different lines (bokeh)

2024/10/18 12:39:18

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 figuresource = 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.

Answer

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 figuresource = 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

https://en.xdnf.cn/q/72993.html

Related Q&A

Cant Install PIL 1.7

I have python 2.7.3 and I want to install PIL 1.7. I downloaded "PIL-1.1.7.win32-py2.7" and try to install it but it shows me an error messege that it cant find python 2.7 in the registry.&qu…

slice/split a layer in keras as in caffe

I have used this converter to convert a Caffe model to Keras. But one of my layers is of type slice and it needs to be converted as well but the converter currently does not support this and raises an …

Can I use regexes within datetime.strptime formats?

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those. Like:from __future__ import print_functionfrom datetime import datetime # …

Sort a list with longest items first

I am using a lambda to modify the behaviour of sort.sorted(list, key=lambda item:(item.lower(),len(item)))Sorting a list containing the elements A1,A2,A3,A,B1,B2,B3,B, the result is A,A1,A2,A3,B,B1,B2,…

Multivariate Root Finding in Python

Using excel solver, it is easy to find a solution (optimum value for x and y )for this equation: (x*14.80461) + (y * -4.9233) + (10*0.4803) ≈ 0However, I cant figure out how to do this in Python. The …

Python print not working when embedded into MPI program

I have an Python 3 interpreter embedded into an C++ MPI application. This application loads a script and passes it to the interpreter.When I execute the program on 1 process without the MPI launcher (s…

Detecting USB Device Insertion on Windows 10 using python

I cant get the following code for Detecting USB Device Insertion to work on my Windows 10 (64 bit) computer with Python 3.7.import win32serviceutil import win32service import win32event import servicem…

I get NotImplementedError when trying to do a prepared statement with mysql python connector

I want to use prepared statements to insert data into a MySQL DB (version 5.7) using python, but I keep getting a NotImplementedError. Im following the documentation here: https://dev.mysql.com/doc/con…

parsing transcript .srt files into readable text

I have a video transcript SRT file with lines in conventional SRT format. Heres an example:1 00:00:00,710 --> 00:00:03,220 Lorem ipsum dolor sit amet consectetur, adipisicing elit.2 00:00:03,220 --…

Sphinx Public API documentation

I have a large number of python file and I would like to generate public API documentation for my project. All the functions that are part of the api I have decorated with a decorator. for example:@api…