Quick Reference · declarative statistical visualization

altair cheat sheet

You don't draw a chart — you declare it. Bind data columns to visual encoding channels (x, y, color…), pick a mark, and Altair emits a Vega-Lite JSON spec that renders in the browser. Learn the grammar — data + mark + encoding — and a huge range of charts comes from one small vocabulary.

import / setup chart · compose · output marks (core) encodings (core) transforms / data interaction / params deprecated / gotcha most common

Distilled & cross-checked against Altair 6.2: altair-viz.github.io (Getting Started + User Guide + API) · vega.github.io/vega-lite · github.com/vega/altair · UW IDL viz-curriculum · Cheatography

The method chain — and how Python becomes a picture
THE FLUENT API — ONE CHAIN, LEFT TO RIGHT alt.Chart(df) the DATA a tidy DataFrame .mark_bar() the MARK point · line · bar · area… .encode(x, y, color) the ENCODINGS columns → visual channels .properties() .interactive() .save() REFINE & OUTPUT size · title · zoom · export WHAT HAPPENS UNDER THE HOOD Altair · Python the chart object .to_dict() / .to_json() builds the spec Vega-Lite spec a JSON document the grammar, serialized Vega runtime compiles + binds data SVG / Canvas rendered in the browser via vega-embed
A complete chart in five lines
import altair as altfrom altair.datasets import data # built-in sample datacars = data.cars() # a tidy pandas DataFramealt.Chart(cars).mark_point().encode( # data + mark + encoding x='Horsepower:Q', # :Q quantitative y='Miles_per_Gallon:Q', color='Origin:N', # :N nominal → distinct colours).interactive() # pan + zoom, then .save('c.html')
01Setup & Importonce per project
02The Chart Objectdata in
03Markshow points look
04Encoding Channelscolumns → visuals
05Encoding Data Typesthe 4 letters
06Channel Optionsalt.X, alt.Y, …
07Aggregation & Binningin the shorthand
08Data Transformsreshape in-spec
09Scales, Axes & Legendsfine control
10Properties & Configwhole-chart looks
11Compositioncombine charts
12Parameters & Selectionsmake it live
13Conditions: when / thenreact to state
14Interactivity Patternsput it together
15Themesglobal styling
16Save & Publishget it out
17Common Chart Recipescopy & adapt
18Data & Big-Data Tipsavoid the traps

Four ideas that make the grammar click

The mental model Altair is built on — the three-part recipe, channels as a mapping from data to visuals, the shorthand string, and how small charts compose into big ones. Based on the Vega-Lite / Altair user-guide reference figures.

data + mark + encoding = chart

Three ingredients. Swap any one and you get a different chart from the same grammar.

a b C4 D3 E6 data + mark bar + encoding x = a:N y = b:Q = chart C D E

channels: columns → visual properties

Each encode() key wires one data column to one visual property. That mapping is the chart.

columns Horsepower MPG Origin channels x y color

the shorthand string

One field string packs three ideas. Learn to read it and most Altair code becomes obvious.

"mean(price):Q" ──── ───── ── aggregate field type :Qquantitative :Nnominal :Oordinal :Ttemporal

composition operators

+ stacks charts in the same frame; | and & place whole charts beside or below each other.

c1 + c2 layer / overlay c1 | c2 hconcat c1 & c2 vconcat

Worth memorizing

data must be tidyone row per observation; transform_fold reshapes wide→long
:Q :N :O :Tthe type sets axis kind, colour scale & sorting
add_params, not add_selectionrenamed in Altair 5
when / then / otherwisethe modern replacement for alt.condition()
selection_point / _intervalreplaced selection_single / selection_multi
+ | &+ layer  ·  | hconcat  ·  & vconcat
MaxRowsError @ 5000enable("vegafusion") or disable_max_rows()
.save png/svgneeds vl-convert-python; HTML needs nothing
mark(color) ≠ encode(color)a constant style vs a mapped column
alt.value(x)a literal constant, never a column name
alt.theme.enable()singular theme since 5.5 (was themes)
to_json() escape hatchedit raw Vega-Lite when Altair lags a feature