Search code examples
pythonbokeh

Bokeh color not appearing on hover tooltip


I'm experimenting with Bokeh and running into a frustrating problem with the hovertool. It lists fill color as one of the basic tooltips covered.

http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hover-tool

I tried a test and the color will not appear on the hovertool. It doesn't even give me the "???" it usually does when you give it a input it doesn't understand, it just ignores it completely. Anyone have a clue as to why it wouldn't display one of the basic tooltips?

enter image description here

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

output_file("toolbar.html")

source = ColumnDataSource(
        data=dict(
            x=[1, 2, 3, 4, 5],
            y=[2, 5, 8, 2, 7],
            desc=['A', 'b', 'C', 'd', 'E'],
        )
    )

hover = HoverTool(
        tooltips=[
            ("fill color", "$color[hex, swatch]:fill_color"),
            ("index", "$index"),
            ("(x,y)", "($x, $y)"),
            ("desc", "@desc"),            
        ]
    )

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Mouse over the dots")


p.circle('x', 'y', size=20, source=source, fill_color="black")

show(p)

Solution

  • Hover tooltips can only inspect values from actual columns in a column data source. Since you have given a fixed value, i.e. fill_color="black" there is no column to inspect. Additionally, the special hover field $color with hex only understands hex color strings.

    Here is your code modified to work:

    from bokeh.plotting import figure, output_file, show, ColumnDataSource
    from bokeh.models import HoverTool
    
    output_file("toolbar.html")
    
    source = ColumnDataSource(
            data=dict(
                x=[1, 2, 3, 4, 5],
                y=[2, 5, 8, 2, 7],
                desc=['A', 'b', 'C', 'd', 'E'],
                fill_color=['#88ffaa', '#aa88ff', '#ff88aa', '#2288aa', '#6688aa']
            )
        )
    
    hover = HoverTool(
            tooltips=[
                ("index", "$index"),
                ("fill color", "$color[hex, swatch]:fill_color"),
                ("(x,y)", "($x, $y)"),
                ("desc", "@desc"),
            ]
        )
    
    p = figure(plot_width=400, plot_height=400, tools=[hover],
               title="Mouse over the dots")
    
    
    p.circle('x', 'y', size=20, source=source, fill_color="fill_color")
    
    show(p)
    

    enter image description here