Altair draws interactive charts in Python from a short description of your data. Pass a DataFrame to alt.Chart(), pick a shape with a method such as mark_point(), mark_bar() or mark_line(), and map columns to the axes and colors with encode(). This tutorial uses Altair 6.2; install it with pip install altair pandas or uv add altair pandas.

Quick Example
Rent against floor area for apartments in three made-up neighborhoods. Hover a point to see its values, drag to pan, and scroll to zoom.
import altair as alt
import numpy as np
import pandas as pd
# Sample data: floor area and monthly rent for 90 apartments.
rng = np.random.default_rng(52)
neighborhood = np.repeat(["Old Town", "Riverside", "Hillcrest"], 30)
area = rng.uniform(35, 120, 90).round()
base = np.repeat([900, 700, 520], 30)
rent = (base + 14 * area + rng.normal(0, 130, 90)).round(-1)
homes = pd.DataFrame({"neighborhood": neighborhood, "area_m2": area, "rent": rent})
chart = alt.Chart(homes, width="container", height=360).mark_circle(size=80).encode(
x=alt.X("area_m2:Q", title="Floor area (m²)"),
y=alt.Y("rent:Q", title="Monthly rent ($)"),
color=alt.Color("neighborhood:N", title=None, legend=alt.Legend(orient="top")),
tooltip=["neighborhood", "area_m2", "rent"],
).interactive()
chart
In Jupyter, VS Code notebooks and Colab, a chart on the last line of a cell displays itself. In a script, save it with chart.save("rent.html") and open the file, or call chart.show().
How Does Altair Work?
Every chart answers three questions:
| Piece | Code | What it sets |
|---|---|---|
| Data | alt.Chart(homes) |
A DataFrame with one row per observation |
| Mark | .mark_circle() |
The shape drawn for the data |
| Encoding | .encode(x=..., y=..., color=...) |
Which column controls position, color, size and tooltips |
Altair turns this into a Vega-Lite specification, which the browser draws. Print chart.to_json() to see it.
Data works best in long form, with one column per variable. If each series has its own column, reshape it with melt() first.
What Do :Q, :N, :O and :T Mean?
The suffix after a column name tells Altair what kind of data it is, which decides the axis and color scale:
| Suffix | Type | Example |
|---|---|---|
:Q |
Quantitative (numbers) | "rent:Q" |
:N |
Nominal (unordered categories) | "neighborhood:N" |
:O |
Ordinal (ordered categories) | "year:O" |
:T |
Temporal (dates and times) | "date:T" |
Altair infers the type from a pandas column's dtype, so the suffix is optional for DataFrame columns. Write it when the guess is wrong, or when the column is created inside the chart.
Which Altair Tutorial Do I Need?
| You want to | Tutorial |
|---|---|
| Understand the basics step by step | A Brief Introduction to Altair |
| Draw bar and line charts | Altair Bar and Line Charts |
| Show details on hover | Scatter Plot Tooltips |
| Set axis titles and number formats | Axis Titles and Formatting |
| Overlay charts or place them side by side | Layering and Concatenation |
| Make small multiples | Facet Charts |
| Brush, click and filter | Interactive Selections |
| Draw a map | Choropleth Maps |
| Restyle every chart at once | Altair Themes |
| Export PNG, SVG or HTML | Save Altair Charts |
How Do I Save an Altair Chart?
chart.save() picks the format from the file extension. HTML and JSON need only Altair, while PNG, SVG and PDF also need the vl-convert-python package.
| Format | Call | Interactive |
|---|---|---|
| HTML | chart.save("rent.html") |
Yes |
| PNG | chart.save("rent.png", scale_factor=2) |
No |
| SVG | chart.save("rent.svg") |
No |
Common Altair Problems
| Problem | Fix | Details |
|---|---|---|
MaxRowsError with more than 5,000 rows |
Aggregate in pandas first, or enable the VegaFusion data transformer | Row limit |
ValueError about a field's encoding type |
Add a suffix such as :Q to a column made by a transform |
Computed fields |
add_selection or selection_single raise deprecation warnings |
They are Altair 4 names; use add_params and selection_point |
Selections |
width="container" doesn't resize a faceted chart |
Give each panel a fixed width | Facets |
| Months sort alphabetically | Pass sort=None to keep the data order |
Axes |
Should I Use Altair, Plotly or Bokeh?
Altair suits statistical charts where you want to state the mapping and let the library handle axes, legends and linked selections. Plotly has 3D charts and a larger set of ready-made chart types, and Bokeh gives more control over drawing and streaming updates. The interactive library comparison puts them side by side.
