Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display the legend name associated with a series in the HoverTool tooltip in Bokeh?

In the code below I would like to know what to put in the place of the "????" so that the hover tool will show the name of the series (in this example either "series 1" or "series 2")

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import HoverTool, ColumnDataSource
output_notebook()

hover = HoverTool()
hover.tooltips=[("series name","????")]

f = figure(tools=[hover])

f.line([1,2,3],[2,1,5],legend="series 1")
f.line([1,2,3],[1,7,2],legend="series 2")

show(f)

I know you can do the following to make this work (see In Bokeh, how do I add tooltips to a Timeseries chart (hover tool)?). However I am embedding the plots in an HTML file that will have many data points per plot and many plots in the file so I am interested in minimizing the size of the data source that gets embedded in the HTML file.

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import HoverTool, ColumnDataSource
output_notebook()

hover = HoverTool()
hover.tooltips=[("series name","@legend")]

f = figure(tools=[hover])

data1 = ColumnDataSource({"x":[1,2,3], "y":[2,1,5], "legend":["series 1"]*3})
data2 = ColumnDataSource({"x":[1,2,3], "y":[1,7,2], "legend":["series 2"]*3})


f.line("x","y",source=data1, legend="series 1")
f.line("x","y",source=data2, legend="series 2")

show(f)
like image 608
Jesse Avatar asked Oct 19 '22 17:10

Jesse


1 Answers

If I understand correctly, what you want to do only works (efficient) if you associate a specific tooltip with a specific renderer. On Github there is an issue with the following comment, giving a small code snippet that does what you want: https://github.com/bokeh/bokeh/issues/3454#issuecomment-168238796

like image 199
osdf Avatar answered Oct 31 '22 17:10

osdf