Robin's Blog

Bokeh plots with DataFrame-based tooltips

Recently I’ve been investigating a key dataset in my research, and really seeking to understand what is causing the patterns that I see. I realisedĀ that it would be really useful if I could plot an interactive scatter plot in Python, and then hover over points to find out further information in them.

Putting this into more technical terms: I wanted a scatter plot with tooltips (or ‘hover boxes’, or whatever else you want to call them) that showed information from the other columns in the pandas DataFrame that held the original data.

I’m fairly experienced with creating graphs using matplotlib, but I have very little experience with other systems (although I had played a bit with bokeh and mpld3). So, I emailed my friend and colleague Max Albert, asking if he knew of a nice simple function that looked a bit like this:

# df is a pandas DataFrame with columns A, B, C and D
#
# scatter plot of A vs B, with a hover tool giving the
# values of A, B, C and D
plot(A, B, 'x')
# same, but only showing C (to deal with DataFrames with
# loads of columns)
plot(A, B, 'x', cols=['C'])

He got back to me saying that he didn’t know of a function that worked quite like that, but that it was relatively simple to do in bokeh – and sent some example code. I’ve now tidied that code up and created a function that is very similar to the example above.

My function is called scatter_with_hover, and using it, the two examples above would be written as:

scatter_with_hover(df, 'A', 'B')
scatter_with_hover(df, 'A', 'B', cols=['C'])

You can pass all sorts of other parameters to the function too – such as marker shapes to use (circles, squares etc), a figure to plot on to, and any other parameters that the bokeh scatter function accepts (such as color, size etc).

Anyway, the code is below – feel free to use it in any way you want, and I hope it’s useful.


Categorised as: Programming, Python


8 Comments

  1. Paul Gowder says:

    This is super-awesome, I just put it to good use in making some nice flashy some supplemental plots for the website for a book. Also a good excuse to try bokeh. Thanks!

  2. […] you liked this post, then you may also likeĀ An easy way to install Jupyter Notebook extensions, Bokeh plots with DataFrame-based tooltips, and Orthogonal Distance Regression in […]

  3. George Crowther says:

    Thank you very much for this, I’ve been trying to work out how to configure my tool tips to display the frame index value, very frustrating!

  4. Architha says:

    Hi, This is so great , this is exactly what i was looking for but my plot seems to be empty when i try this , did you run into something like this ?

  5. Robin Wilson says:

    Unfortunately, I haven’t run into that – and I’m not a huge expert on Bokeh. Does it work with a standard Bokeh plot without my code? If so, it might be worth taking the code I gave and gradually removing pieces until it starts working – there could well be a bug in my code!

    Good luck!

  6. Joey says:

    Hi there,

    How do you handle df columns that have a space in the name? My work involves a lot of existing data and renaming columns is not an option.

  7. Robin Wilson says:

    Hi,

    You should be able to access these columns using square brackets, such as df['column name here'], as opposed to the standard df.columnname. Alternatively, it is possible to rename the columns within pandas.

    Cheers,

    Robin

Leave a Reply

Your email address will not be published. Required fields are marked *