Key Takeaways: Altair
-
The Grammar of Graphics is the theoretical foundation. Wilkinson's 1999 framework (data + marks + encodings + scales + transforms + coordinates + facets) underlies ggplot2, Vega-Lite, and Altair. Every chart in the grammar-of-graphics tradition is a composition of these primitives.
-
Altair generates Vega-Lite specs. Altair is a Python binding that produces JSON specifications in the Vega-Lite language. The Vega-Lite JavaScript library then renders the specs in the browser. Python describes, JavaScript renders — similar to Plotly but with a different grammar underneath.
-
The canonical pattern is
alt.Chart(data).mark_*().encode(...). Every Altair chart starts with the data, chooses a mark (point, line, bar, area, rect, tick, rule, text, geoshape, boxplot, errorbar, etc.), and specifies aesthetic mappings via.encode(x, y, color, size, shape, opacity, tooltip, row, column, ...). -
Data types are specified with shorthand suffixes.
:Q(quantitative),:N(nominal),:O(ordinal),:T(temporal). Explicit types prevent bugs and control Altair's scale and axis defaults. Always specify the type even when Altair could infer it. -
Mark-level styling differs from encoding-level mapping. Pass colors, sizes, and shapes as mark arguments (
mark_point(color="blue")) when they are constant; pass them as encodings (encode(color="Origin:N")) when they vary by data. Mixing them up is a common beginner mistake. -
Selections enable interactive filtering and linked views.
alt.selection_interval()creates a brush;alt.selection_point()creates a click selection. Attach the selection to the chart with.add_params(selection)and use it inalt.condition(...)ortransform_filter(selection)to make the chart (and other charts) respond to user interaction. -
Composition operators are Altair's secret weapon.
+layers,|concatenates horizontally,&concatenates vertically,.facet()creates small multiples,.repeat()builds pair plots. These operators compose, so complex dashboards are a few lines of Altair code when they would be hundreds of lines in matplotlib. -
Data transformations happen inside the chart spec.
transform_filter,transform_calculate,transform_aggregate,transform_fold,transform_window,transform_bin, and others let you reshape and compute data as part of the Altair spec. Transforms re-run when selections change, powering linked views without any Python state management. -
The 5000-row limit is the most famous gotcha. Altair throws
MaxRowsErroron DataFrames with more than 5000 rows. Fix withalt.data_transformers.disable_max_rows()for small datasets oralt.data_transformers.enable("vegafusion")for genuinely large ones. -
Declare, don't instruct. Altair's threshold concept — "declaration over instruction" — means you describe what the chart should look like rather than how to draw it. Once internalized, complex visualizations become compositions of simple pieces, the same way complex SQL queries are compositions of simple clauses. The compositionality is the power.
With Chapter 22 complete, Part V (Interactive Visualization) is finished. You now have three complementary interactive libraries in your toolkit: Plotly Express for quick interactive charts, Plotly Graph Objects for complex custom interactive figures, and Altair for grammar-of-graphics declarative charts with linked views. Chapter 23 begins Part VI (Specialized Domains) with geospatial visualization — maps, choropleths, and location-based data.