Bokeh Tutorial: Interactive Charts in Python

Make your first Bokeh plot, understand figures, glyphs and data sources, save the result as HTML, and find the tutorial for each task.

Bokeh draws interactive charts in the browser from Python. Create a plot with figure(), add shapes to it with methods such as line(), scatter() or vbar(), and call show(). This tutorial uses Bokeh 3.10; install it with pip install bokeh or uv add bokeh.

Grid of six Bokeh charts: lines over time, a shaded band, grouped bars, stacked bars, a hover tooltip and linked scatter plots

Quick Example

Average monthly rainfall for two made-up weather stations. The chart pans and zooms, and hovering shows the values.

from bokeh.plotting import figure, show

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
coast = [182, 150, 131, 94, 78, 85, 102, 134, 171, 208, 215, 196]
inland = [41, 36, 44, 58, 76, 69, 48, 39, 52, 66, 59, 47]

p = figure(x_range=months, title="Monthly rainfall", y_axis_label="mm",
           height=400, sizing_mode="stretch_width",
           tooltips="$name, @x: @y mm")
for name, values, color in [("Coast", coast, "#1864ab"), ("Inland", inland, "#d9480f")]:
    p.line(months, values, name=name, legend_label=name, color=color, line_width=3)
    p.scatter(months, values, name=name, legend_label=name, color=color, size=9)
p.y_range.start = 0
p.legend.location = "top_center"
show(p)

Bokeh line chart of monthly rainfall; the coastal station stays above the inland one all year and peaks at 215 mm in November

The coastal station is wettest in autumn, peaking at 215 mm in November, and the inland one peaks in May at 76 mm. In a script, show() writes an HTML file and opens it in your browser.

How Bokeh Plots Are Built

Piece What it is Example
Figure The plot area, with axes, grid and toolbar p = figure(height=400)
Glyph A shape drawn from data p.scatter(), p.line(), p.vbar(), p.patch()
Data source A table of columns that glyphs read ColumnDataSource, or a DataFrame
Tool An interaction in the browser pan, box zoom, hover, box select
Layout Several plots and widgets together row(), column(), gridplot()

Plot a DataFrame With a Data Source

Pass a DataFrame as source and refer to its columns by name. Size and color can come from columns as well.

import numpy as np
import pandas as pd
from bokeh.transform import linear_cmap

rng = np.random.default_rng(17)
cities = pd.DataFrame({
    "density": rng.uniform(1.0, 11.0, 70),
    "population": rng.lognormal(12.6, 0.8, 70),
})
cities["transit"] = (8 + 4.2 * cities["density"] + rng.normal(0, 6, 70)).clip(2)
cities["size"] = np.sqrt(cities["population"]) / 40 + 4

p = figure(title="Transit use rises with density (sample cities)", height=400,
           sizing_mode="stretch_width", x_axis_label="Thousand people per km²",
           y_axis_label="Commuters using transit (%)",
           tooltips=[("Population", "@population{0,0}"), ("Transit", "@transit{0}%")])
mapper = linear_cmap("transit", "Viridis256", cities["transit"].min(),
                     cities["transit"].max())
points = p.scatter("density", "transit", source=cities, size="size", color=mapper,
                   line_color="#495057", fill_alpha=0.85)
p.add_layout(points.construct_color_bar(title="Transit (%)"), "right")
show(p)

Bubble scatter of sample cities in which transit use rises with density, with a color bar for transit share

Marker size follows population and color follows transit share. size is in screen pixels, which is why the code takes a square root and rescales: the marker area then grows roughly with population.

Style the Chart

Titles, axes, grids and the background are attributes of the figure.

p = figure(title="Weekly sign-ups", height=340, sizing_mode="stretch_width",
           toolbar_location=None, background_fill_color="#f8f9fa")
weeks = np.arange(1, 25)
signups = 40 + 3.2 * weeks + rng.normal(0, 8, 24)
p.varea(weeks, 0, signups, fill_color="#099268", fill_alpha=0.25)
p.line(weeks, signups, line_color="#099268", line_width=3)

p.title.text_font_size = "18px"
p.title.align = "center"
p.xaxis.axis_label = "Week"
p.yaxis.axis_label = "Sign-ups"
p.axis.axis_label_text_font_style = "bold"
p.xgrid.visible = False
p.ygrid.grid_line_dash = [4, 4]
p.outline_line_color = None
p.y_range.start = 0
show(p)

Styled area chart of weekly sign-ups rising over 24 weeks

For a whole-document look, set a built-in theme before showing: curdoc().theme = "dark_minimal", with curdoc imported from bokeh.io. The other built-in themes are light_minimal, caliber, carbon, night_sky and contrast.

Set the Size and Choose Tools

A figure is 600 × 600 pixels unless you set width and height. sizing_mode="stretch_width" fills the width of the page or notebook cell and keeps the height.

The default toolbar has pan, wheel zoom, box zoom, save, reset and help. Wheel zoom is inactive until you click it, so the page can scroll. Pick your own set with a comma-separated string, for example tools="pan,box_zoom,hover,reset", or hide the toolbar with toolbar_location=None.

Show Bokeh Plots in Jupyter

Call output_notebook() once, then show() draws below the cell.

from bokeh.io import output_notebook
from bokeh.plotting import figure, show

output_notebook()

p = figure(height=250)
p.line([1, 2, 3], [4, 6, 5])
show(p)

Save to HTML or PNG

save() writes an HTML file without opening it. By default the file loads BokehJS from Bokeh's CDN, so it needs an internet connection; resources=INLINE bundles the JavaScript into the file.

from bokeh.io import save
from bokeh.resources import CDN, INLINE

q = figure(height=250, title="Saved chart")
q.vbar(x=[1, 2, 3], top=[3, 5, 2], width=0.7)

save(q, filename="chart.html", resources=CDN, title="Chart")
save(q, filename="chart-offline.html", resources=INLINE, title="Chart")

For a PNG, use export_png(q, filename="chart.png") from bokeh.io. It renders the chart in a headless browser, so it needs either Selenium with a matching browser driver, or Playwright. To put a chart in your own web page, see embedding interactive charts in a website.

Where to Go Next

You want to Tutorial
Plot trends, dates and bands Line charts
Compare categories, group and stack Bar charts
Control what appears on hover Hover tooltips
Connect several plots or add a slider Linked plots
Draw millions of points Datashader
Get Bokeh charts from df.hvplot hvPlot

Bokeh or Plotly?

Choose When
Bokeh You want to control individual shapes and tools, link plots, or run Python callbacks with bokeh serve
Plotly You want one function per chart type, or need 3D, maps or financial charts
hvPlot You want Bokeh output from one line on a DataFrame

The interactive charting libraries comparison draws one chart in each.

Common Bokeh Problems

Problem Cause and fix
Nothing appears in a notebook Call output_notebook() before show()
A bar chart is empty Pass the category names as x_range
A tooltip shows ??? The column isn't in the glyph's data source
circle(size=...) warns Use scatter(size=...); the circle form is deprecated since 3.4
Models must be owned by only a single document A plot or source was reused in a second output

Similar Topics