Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bokeh Utils
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool
def scatter_with_hover(df, x, y,
fig=None, cols=None, name=None, marker='x',
fig_width=500, fig_height=500, **kwargs):
Plots an interactive scatter plot of `x` vs `y` using bokeh, with automatic
tooltips showing columns from `df`.
df : pandas.DataFrame
DataFrame containing the data to be plotted
x : str
Name of the column to use for the x-axis values
y : str
Name of the column to use for the y-axis values
fig : bokeh.plotting.Figure, optional
Figure on which to plot (if not given then a new figure will be created)
cols : list of str
Columns to show in the hover tooltip (default is to show all)
name : str
Bokeh series name to give to the scattered data
marker : str
Name of marker to use for scatter plot
Any further arguments to be passed to fig.scatter
Figure (the same as given, or the newly created figure)
fig = scatter_with_hover(df, 'A', 'B')
fig = scatter_with_hover(df, 'A', 'B', cols=['C', 'D', 'E'], marker='x', color='red')
Robin Wilson <>
with thanks to Max Albert for original code example
# If we haven't been given a Figure obj then create it with default
# size etc.
if fig is None:
fig = figure(width=fig_width, height=fig_height, tools=['box_zoom', 'reset'])
# We're getting data from the given dataframe
source = ColumnDataSource(data=df)
# We need a name so that we can restrict hover tools to just this
# particular 'series' on the plot. You can specify it (in case it
# needs to be something specific for other reasons), otherwise
# we just use 'main'
if name is None:
name = 'main'
# Actually do the scatter plot - the easy bit
# (other keyword arguments will be passed to this function)
fig.scatter(x, y, source=source, name=name, marker=marker, **kwargs)
# Now we create the hover tool, and make sure it is only active with
# the series we plotted in the previous line
hover = HoverTool(names=[name])
if cols is None:
# Display *all* columns in the tooltips
hover.tooltips = [(c, '@' + c) for c in df.columns]
# Display just the given columns in the tooltips
hover.tooltips = [(c, '@' + c) for c in cols]
hover.tooltips.append(('index', '$index'))
# Finally add/enable the tool
return fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment