jdbul33
jdbul33

Reputation: 195

How to get hovertool to show another value from plotted dataframe in Bokeh?

I am trying to plot a jitter plot in Python Bokeh, and the plot turns out fine. I am having trouble getting the hovertool to display what I want. The plot show number of calls on y-axis, and department number on the x-axis. I am attempting to get the hover tool to show the associated topic for each point. Here is my code:

p6 = figure(title = 'Number of Calls by Topic for Busiest Departments',
        y_range=(0,600), tools="hover", tooltips="@Topic")

for i, d in enumerate(list(df_6['Dept'].unique())):
    y = df_6[df_6['Dept'] == d][['Count', 'Topic']]
    color = colors[i  % len(colors)]
    p6.circle(x={'value': i, 'transform': Jitter(width=0.4)}, y=y['Count'], 
    color=color)

So I iterate to create sub data frames for each department, keeping 'Topic' associated with each point. When I show the plot, the hovertool just displays ??? instead of the topic. How can I get it to display the topic associated with each point? Thanks!

Upvotes: 2

Views: 787

Answers (1)

bigreddot
bigreddot

Reputation: 34568

Are you plotting a single circle for very individual point? That is going to be horribly inefficient with even a small number of points. Bokeh glyphs such as circle are intended and optimized to operate on entire lists/arrays of data at a time.

Additionally, if you just pass literal values directly to circle then that's all Bokeh knows to send to the browser. If you want to send extra data columns, e.g. to drive a hover tooltip, then you have to tell Bokeh that those columns need to be sent, by putting them in a data source explicitly, and passing that data source as the source argument to the glyph function. As a convenience, you can also pass Pandas data frames directly as a source and they will be converted to a Bokeh ColumnDataSource automatically.

Since your code is not complete, it's not possible to offer you a direct solution. But here is a complete example with hover and jitter that works with Bokeh >= 0.13.0 and is hopefully illustrative enough:

from bokeh.transform import jitter
from bokeh.plotting import figure, show
from bokeh.sampledata.autompg import autompg

p = figure(plot_width=600, plot_height=300, tooltips="MPG: @mpg")
p.circle(x=jitter('yr', width=0.6, range=p.x_range), y='mpg',
         alpha=0.6, size=10, source=autompg)

show(p)

enter image description here

Upvotes: 1

Related Questions