Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96458

Using a specific column to populate tooltips in Bokeh

Consider the following scatter plot example. I have a dataframe df with three columns: colA, colB and colC. I would like to do a scatterplot of df so that the tooltip shows the values of colC.

I tried:

import bokeh
from bokeh.charts import Scatter, output_file, show
from bokeh.models import HoverTool

p = Scatter(df, x='colA', y='colB', title="Foo", color="navy",
            xlabel="A", ylabel="B", tools="hover")

hover = p.select(dict(type=HoverTool))
hover.tooltips = [("C", "$colC")]
hover.mode = 'mouse'
output_file("scatter.html")
show(p)

but C always shows as ???? in the tooltip. Why?

Upvotes: 2

Views: 924

Answers (1)

bigreddot
bigreddot

Reputation: 34628

The $ syntax is only for a few specific special pre-defined variables. You want to use @foo to reference a generic column in a data source:

from bokeh.plotting import figure, output_file, show

p = figure(tools="hover")

p.circle(x='colA', y='colB', title="Foo", color="navy", source=df)

p.hover.tooltips = [("C", "@colC")]
p.hover.mode = 'mouse'

output_file("scatter.html")
show(p)

See, e.g. http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hovertool

Upvotes: 3

Related Questions