holoviews
holoviews copied to clipboard
Include label in hover tool
It would be nice to be able to easily include the label for a given, e.g., Curve in the hovertool. I tried doing so like this
import numpy as np
import holoviews as hv
from bokeh.models import HoverTool
hv.extension("bokeh")
hover = HoverTool(tooltips=[("label", "$name"), ("x", "@x"), ("y", "@y")])
hv.opts.defaults(hv.opts.Curve(tools=[hover]))
x = np.linspace(-5, 5, 101)
hv.Curve((x, np.sin(x)), label="sin") * hv.Curve((x, np.cos(x)), label="cos")
but my hovertool looks like:

It does seem like there is a name attribute, because if I do
hover = HoverTool(tooltips=[("label", "$random_thing"), ("x", "@x"), ("y", "@y")])
instead, the hovertool doesn't work at all. Perhaps the label from the hv.Curve needs to be passed back to a bokeh glyph for this to work? Or is there a different way that I could reference the label in the hovertool?
One solution
import hvplot.pandas
import pandas as pd
import numpy as np
x = np.linspace(-5, 5, 101)
df = pd.DataFrame({'sin': np.sin(x), 'cos': np.cos(x), 'x': x}).melt(
id_vars='x', value_name='y', var_name='label')
df.hvplot('x', 'y', groupby='label').overlay('label')

ahuang11's solution is not good for me since I would need to create a column containing the line series name.
My preference: Since there is already an existing label= parameter that you can pass into a hvplot call and is being used by the legend, similarly the tooltip, should just grab that to be default "label" as well.
If present, I think including the label and group should be the default behavior for the hover tooltips.