Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 31 ยท Practice and Review

Plotly, Bokeh and Altair Cheat Sheet

One page with the same bar, line, scatter and histogram chart written in Plotly, Bokeh and Altair, plus a chart-choice table and a library picker.

By now you have met three libraries that answer the same question in three different voices. Plotly builds a figure out of traces and a layout. Bokeh builds a plot out of glyphs that read from a data source. Altair describes the chart as data, mark and encodings. This page puts the four most common charts next to each other so you can find the line you need without rereading a lesson.

Plotly, Bokeh and Altair cannot run on this site, so every library snippet below is read-only. The runnable examples are small Python stand-ins that show the shape of what each library produces: a Plotly-style figure dictionary, a Bokeh-style glyph description and a Vega-Lite-style spec. They are simplified, and the text says where the real libraries do more.

All snippets use the same tiny data, so the differences are the library and not the numbers.

Python
import pandas as pd

sales = pd.DataFrame({
    "city": ["Oslo", "Lima", "Kyoto", "Cairo"],
    "revenue": [120, 95, 150, 80],
})

The mental model in one slide

Three ways to say a chart

Plotly A figure is traces plus a layout.
Bokeh A plot is glyphs reading a data source.
Altair A chart is data, mark and encodings.

All three end up as JSON that a JavaScript library draws.

1 / 2

Bar chart

A bar chart compares one number across categories.

Plotly Express

Python
import plotly.express as px

fig = px.bar(sales, x="city", y="revenue", title="Revenue by city")
fig.show()

Bokeh

Python
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

source = ColumnDataSource(sales)
p = figure(x_range=list(sales["city"]), title="Revenue by city", height=300)
p.vbar(x="city", top="revenue", width=0.8, source=source)
show(p)

Altair

Python
import altair as alt

chart = alt.Chart(sales, title="Revenue by city").mark_bar().encode(
    x="city:N",
    y="revenue:Q",
)
chart

Bokeh is the only one of the three that needs you to list the categories up front (x_range) for a categorical axis. Plotly and Altair read them from the data.

Line chart

A line chart shows a value over an ordered axis, usually time. Assume a daily DataFrame with a date column and a visits column.

Plotly Express

Python
fig = px.line(daily, x="date", y="visits")

Bokeh

Python
p = figure(x_axis_type="datetime", height=300)
p.line(x="date", y="visits", source=ColumnDataSource(daily))

Altair

Python
alt.Chart(daily).mark_line().encode(x="date:T", y="visits:Q")

Two things to remember. In Bokeh you must ask for a date axis with x_axis_type="datetime"; in Altair the :T suffix does the same job. With several series in one DataFrame, Plotly and Altair take a color argument and draw one line per value, while Bokeh needs one p.line call per series (or multi_line).

Scatter chart

A scatter chart shows how two numbers relate. Assume a penguins DataFrame with bill, mass and species.

Plotly Express

Python
fig = px.scatter(penguins, x="bill", y="mass", color="species")

Bokeh

Python
p = figure(height=300)
p.scatter(x="bill", y="mass", source=ColumnDataSource(penguins), size=8)

Altair

Python
alt.Chart(penguins).mark_circle().encode(
    x="bill:Q", y="mass:Q", color="species:N", tooltip=["species", "mass"]
)

Here the libraries differ in how much they do for you. Plotly Express and Altair build the legend and the color mapping from color="species". Bokeh gives you the pieces: you map colors yourself, for example with factor_cmap, and add a legend on purpose.

Histogram

A histogram shows how the values of one column are distributed.

Plotly Express

Python
fig = px.histogram(penguins, x="mass", nbins=20)

Bokeh

Python
import numpy as np

counts, edges = np.histogram(penguins["mass"], bins=20)
p = figure(height=300)
p.quad(top=counts, bottom=0, left=edges[:-1], right=edges[1:])

Altair

Python
alt.Chart(penguins).mark_bar().encode(
    alt.X("mass:Q").bin(maxbins=20),
    y="count()",
)

This is where the three philosophies show most clearly. Plotly and Altair do the binning for you, in the browser or in Vega-Lite. Bokeh draws shapes and nothing else, so you compute the bins in NumPy and hand it finished rectangles.

Same chart, three specs

This stand-in builds the bar chart from the same rows in the three styles. It is not the real libraries, but the shapes match what they hold internally: Plotly keeps column lists inside a trace, Bokeh keeps a source plus a glyph, and Vega-Lite keeps rows plus encodings.

Python One bar chart, three spec shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that Plotly and Bokeh store the data as columns (one list per field), while Vega-Lite stores it as rows (one object per record). That is a real difference, and it is why converting data between the libraries sometimes needs a reshape.

Binning by hand

All three histograms hide the same small algorithm: cut the range into equal-width intervals and count the values in each. The stand-in below does it in plain Python so you can see what bins=5 means.

Python Equal-width histogram bins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real libraries choose bin edges differently. Plotly picks "nice" automatic edges unless you set nbins, np.histogram uses equal-width edges like this stand-in, and Vega-Lite's maxbins is an upper limit: it rounds the step to a friendly number such as 5 or 10, so you can get fewer bins than you asked for.

Which chart for which question

Question Chart Plotly Express Bokeh Altair mark
How do categories compare? Bar px.bar vbar mark_bar
How does a value change over time? Line px.line line mark_line
How do two numbers relate? Scatter px.scatter scatter mark_circle / mark_point
How is one number distributed? Histogram px.histogram quad + NumPy mark_bar with bin
How is a number spread per group? Box px.box vbar + segment (manual) mark_boxplot
How are two categories related by a value? Heatmap px.density_heatmap / px.imshow rect mark_rect
What share does each part have? Stacked bar (pie only for 2-3 parts) px.bar with color vbar_stack mark_bar with color

The choosing a chart lesson explains the reasoning behind these pairings, and color and accessibility covers the palette for each.

A tiny helper can encode the first half of that table. This stand-in is a lookup, not a recommendation engine, but it shows how the question decides the chart.

Python From question to chart

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Picking a library

You need Good fit Why
A quick interactive chart from a DataFrame Plotly Express One call, hover and zoom included
A web dashboard in pure Python Dash (Plotly) or Bokeh server Both have callbacks and widgets
Many linked views from short code Altair Selections are part of the grammar
Full control over every glyph Bokeh Low-level drawing and streaming data
A spec that other tools and languages can read Altair / Vega-Lite The chart is plain JSON
Very large point counts Plotly scattergl or Bokeh with aggregation WebGL or server-side reduction (see large datasets)

Altair embeds the data in the spec, so by default it raises an error above 5,000 rows. Past that limit, aggregate in pandas first or pass a URL to the data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which library requires you to compute histogram bins yourself, for example with NumPy?

    Bokeh draws glyphs from finished numbers. Plotly (px.histogram) and Altair (bin plus count) compute the bins for you.
  2. 2. How does Altair mark a column as dates so the axis becomes a time axis?

    Altair uses data type suffixes (Q, N, O, T). The x_axis_type argument belongs to Bokeh figures.
  3. 3. Which pair describes how data is stored inside a Plotly trace and a Vega-Lite spec?

    A Plotly trace keeps one list per field, such as x and y. Vega-Lite keeps a list of record objects.
  4. 4. You want to answer how one number is distributed across 10,000 values. Which chart is the usual first choice?

    A histogram bins one numeric column and shows how many values fall into each range.

Summary

  • Plotly builds figures from traces and a layout, Bokeh from glyphs and a data source, Altair from data, a mark and encodings.
  • The same four charts take one line in Plotly Express and Altair, and a few more in Bokeh, which gives you the building blocks.
  • Bokeh needs explicit category ranges, date axis types, color mapping and bin computation; Plotly and Altair infer most of this.
  • Plotly and Bokeh hold data in columns, while Vega-Lite holds rows.
  • Match the chart to the question first (compare, trend, relationship, distribution), then the library to the job.
  • The code for the real libraries on this page is read-only; the Python stand-ins only mirror the shapes of their specs.

You have reached the end of the track. To practice, return to the exercises or review the interview questions.