Altair
Altair: Declarative Visualization for Python
Overview
Altair is a statistical visualization library for Python, based on Vega-Lite. It is "Declarative", meaning you describe *what* you want the chart to look like (mapping columns to visual channels), not *how* to draw lines and pixels.
The Grammar of Graphics
You map data columns to channels:
- x / y: Position.
- color: Color.
- size: Size.
- shape: Shape.
Example
import altair as alt
from vega_datasets import data
cars = data.cars()
chart = alt.Chart(cars).mark_circle().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
tooltip=['Name', 'Origin']
).interactive()Pros
- Consistent API: Once you learn the grammar, you can build any chart.
- Interactivity: Zoom/Pan/Tooltip is one line (`.interactive()`).
- JSON: The output is a JSON spec (Vega-Lite), which can be easily embedded in websites.
Cons
- Large Data: Since it embeds the data into the JSON, plotting >5,000 points can crash the browser. (Workarounds exist using Altair Saver or VegaFusion).