Altair is a declarative Python library for statistical visualization, built on the Vega and Vega-Lite visualization grammars. Rather than programming every drawing step, describe the chart you want: provide data, choose a visual form, and map fields to visual channels. Altair then creates the visualization, including much of the chart structure and styling. section 2.1 section 3.4
A chart is built from three core pieces: a chart supplies the dataset, a mark chooses the visual form, and encodings map data attributes to channels such as x, y, color, and size. Marks can be bars, points, lines, or areas. This structure keeps the code focused on the intended visual meaning rather than manual drawing operations. section 2.5 section 3.4
A declarative line chart
Create a DataFrame, pass it to alt.Chart(), choose mark_line(), and encode x and y fields. Add a title through properties(). The resulting specification states that x and y should appear as a line chart; it does not manually place axes, labels, and styling as an imperative plotting workflow does. section 1.2 section 2.1.1
Altair contrasts with imperative libraries such as Matplotlib. Imperative plotting requires explicit steps to produce a chart, while Altair centers the desired output. This does not make Altair universally preferable: an imperative approach remains valid when it is the preferred way to construct a visualization, and data-storytelling principles can apply to either approach. section 1.2 section 2.3.4
Use further Altair components to refine meaning and interaction:
- Conditions change visual properties according to criteria. For example, color bars red when Region is Europe and blue otherwise with alt.condition(). section 3.2.3
- Compound charts combine multiple charts in one view, while interactivity lets a chart respond to user input. section 3.4
- Configurations adjust presentation details such as axes, scales, and layout to better convey information or aesthetics. section 3.4
For data storytelling, progress from raw data toward information, knowledge, and wisdom: clean the dataset, extract meaningful information, visualize it, then add context and a call to action through annotations. Interactivity can let an audience explore the data directly, but the chart still needs a purposeful message for that audience. section 2.5 section 1.1
Altair is suited to quickly building charts from modest datasets. It can build charts with more than 5,000 rows, but performance slows; complex exploratory analysis, big-data analytics, and detailed big-data reports call for stronger processing or analytics platforms. Preprocess data below that threshold when practical before using Altair for storytelling. section 1.3
Listing 1.2 Declarative library
import altair as alt
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [1, 4, 9, 16, 25]})
chart = alt.Chart(df).mark_line().encode(
x='x',
y='y'
).properties(
title='Square Numbers'
)
chart.save('chart.png')
Inspect a complete minimal specification: DataFrame input, line mark, x/y encodings, title, and saved output. section 1.2
