Line Color in HoverTool

I have a plot with multiple lines. Now I want that the HoverTool shows the color of the corresponding line, when hovering over this line.
So I put all the colors in in a list and then into the ColumnDataSource. However, the HoverTool says ‘color unknown’. Do you see my mistake?

source = ColumnDataSource(data=source_dict)
...
colors = []
lines = []
for curr,color in zip(range(len(currencies)),color_palette):
    line = p.line(x='dates', y=currencies[curr], line_width=2, alpha=1, color=color, name=currencies[curr], legend_label=currencies[curr], source=source)
    lines.append(line)
    colors.append(color)

source_dict['color'] = colors


hover = HoverTool(tooltips=[("date", "@dates{%F}"),
                            ('currency','$name'),
                            ('return','@$name{0.00%}'),
                            ('line color', "$color[hex, swatch]:color")],
                  formatters={'@dates': 'datetime'})

Hi @loorenaa,

The color option in a HoverTool is really intended for plots where each data point has an allocated color, rather than a line color as you’re trying to do here.

You may want to try using multi_line instead, if that works for your data. Here’s an example with a working color hover:

from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure, show

x = [0, 1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 4, 6, 5, 3, 2, 4]
z = [6, 2, 8, 4, 1, 1, 3, 2]
colors = ['#D5753F', '#2D7268']

source = ColumnDataSource(data=dict(xs=[x, x], ys=[y, z], colors=colors))

hover = HoverTool(tooltips=[('line color', "$swatch:colors")])

p = figure(tools=[hover])
p.multi_line(xs="xs", ys="ys", source=source, width=3, line_color="colors")
show(p)

If that sort of thing won’t work for you, please post a Minimal, Reproducible Example of the issue so reviewers can test and observe the problem.

1 Like