HoverTool for multiple data series in a bokeh scatter plot

I have the following small example script using numpy and bokeh:

import numpy as np import bokeh.plotting as bp from bokeh.objects import HoverTool bp.output_file('test.html') fig = bp.figure(tools="reset,hover") x = np.linspace(0,2*np.pi) y1 = np.sin(x) y2 = np.cos(x) 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') s2.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"} bp.show() 

The problem is that the hover tool only works for the cosine curve, but not for the sine.

I know that one option would be to build both a series of togehter and change the color of the cosine data points:

 import numpy as np import bokeh.plotting as bp from bokeh.objects import HoverTool bp.output_file('test.html') fig = bp.figure(tools="reset,hover") x = np.linspace(0,2*np.pi) y1 = np.sin(x) y2 = np.cos(x) x = np.array([x,x]).flatten() y = np.array([y1,y2]).flatten() blue = np.array('#0000ff').flatten() red = np.array('#ff0000').flatten() colors = np.array([blue.repeat(len(y1)),red.repeat(len(y1))]).flatten() s1 = fig.scatter(x=x,y=y,color=colors,size=10,legend='sine & cosine') s1.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"} bp.show() 

But then I lose the legend record for the second color.

How can I hover over both datasets and see the corresponding prompt?

Thanks!

Max

+13
python bokeh
source share
2 answers

This is actually a mistake resolved in the wizard. I was fixed in this PR https://github.com/bokeh/bokeh/pull/1511

In addition, you will need to modify your first code to use models instead of objects in the third line, as shown below:

 import numpy as np import bokeh.plotting as bp from bokeh.models import HoverTool bp.output_file('test.html') fig = bp.figure(tools="reset,hover") x = np.linspace(0,2*np.pi) y1 = np.sin(x) y2 = np.cos(x) 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"} bp.show() 

Hope this helps!

Greetings.

Damian

UPDATED with comments below

+11
source share

If you want to have several guidance tools, you need to add several hover tools, each of which is configured for a different visualization tool. You can add them as follows:

 p = figure() r1 = p.circle([1,2,3], [4,5,6], color="blue") p.add_tools(HoverTool(renderers=[r1], tooltips=TIPS)) r2 = p.square([4,5,6], [1,2,3], color="red") p.add_tools(HoverTool(renderers=[r2], tooltips=TIPS)) 
+25
source share

All Articles