holoviz / holoviews

With Holoviews, your data visualizes itself.
https://holoviews.org
BSD 3-Clause "New" or "Revised" License
2.69k stars 402 forks source link

Include label in hover tool #3603

Open neighthan opened 5 years ago

neighthan commented 5 years ago

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:

image

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?

ahuang11 commented 5 years ago

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')

image

pybokeh commented 4 years ago

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.

droumis commented 7 months ago

If present, I think including the label and group should be the default behavior for the hover tooltips.