Introduction
Altair is a declarative statistical visualization library for Python built on top of the Vega-Lite specification. Instead of writing imperative drawing commands, you describe the relationship between data fields and visual properties, and Altair compiles the description into an interactive Vega-Lite JSON spec rendered in the browser or exported to PNG, SVG, or PDF.
What Altair Does
- Creates statistical visualizations from pandas DataFrames using a concise declarative syntax
- Generates interactive charts with tooltips, selections, and cross-filtering built in
- Compiles Python chart definitions to portable Vega-Lite JSON specifications
- Supports layering, concatenation, faceting, and repeating of chart compositions
- Exports charts to HTML, PNG, SVG, and PDF formats
Architecture Overview
Altair is a thin Python API that generates Vega-Lite JSON specifications. When you call mark_point().encode(x, y), Altair builds a JSON object describing the chart. This spec is rendered by the Vega-Lite JavaScript engine in Jupyter notebooks, web browsers, or headless renderers. The separation between specification and rendering means the same chart definition works across all output formats and frontends without modification.
Self-Hosting & Configuration
- Install via pip; add vl-convert-python for static PNG/SVG export without Node.js
- Works out of the box in Jupyter Notebook, JupyterLab, VS Code, and Colab
- Configure default themes with alt.themes.enable() for consistent styling
- Set alt.data_transformers.enable('vegafusion') for large datasets exceeding 5000 rows
- Use alt.renderers.enable() to switch between notebook, HTML, and static renderers
Key Features
- Concise grammar: complex charts in 3-5 lines of Python code
- Built-in interactivity: selections, tooltips, zooming, and linked brushing
- Automatic type inference maps DataFrame columns to appropriate visual encodings
- Composable charts via layering (+), horizontal (|), and vertical (&) concatenation operators
- Full access to the Vega-Lite ecosystem including custom themes and extensions
Comparison with Similar Tools
- Matplotlib — imperative and highly customizable; Altair is more concise for statistical plots
- Seaborn — statistical plots on Matplotlib; Altair adds interactivity and a declarative grammar
- Plotly — rich interactive charts with a broader widget set; Altair's Vega-Lite foundation is more composable
- Bokeh — server-backed interactive dashboards; Altair produces standalone specs without a server
- ggplot2 (R) — Altair's grammar of graphics is directly inspired by ggplot2, adapted for Python and the web
FAQ
Q: Can Altair handle large datasets? A: By default, Altair warns above 5000 rows. Enable the VegaFusion transformer or pre-aggregate data to handle millions of rows efficiently.
Q: Does Altair work outside Jupyter? A: Yes. Save charts as HTML files for the browser, or use vl-convert to export static PNG, SVG, or PDF images for reports and publications.
Q: How do I create dashboards with Altair? A: Combine charts using concatenation and add interactive selections for cross-filtering. For full dashboards, pair Altair with Streamlit or Panel.
Q: Can I customize the look and feel? A: Yes. Altair supports themes, custom color scales, axis formatting, and direct Vega-Lite config overrides for fine-grained control.