# Altair Tutorial: Interactive Charts in Python

> An Altair tutorial for Python: install Vega-Altair, draw a first interactive chart with mark_point() and encode(), learn the data types, save charts, and fix common errors.

Source: https://www.pythoncharts.com/altair/altair-tutorial/ | Published: 2026-11-26 | Author: Alex Scott | Category: Altair | Tags: altair, guide, interactive charts, tutorial, vega-lite

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`.

![Grid of six Altair charts: grouped bar chart, labeled lines, small multiples, a brushed selection, a US state map and a custom theme](https://www.pythoncharts.com/altair/altair-tutorial/images/altair-tutorial-montage.png)

## 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.

```python
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
```

![Altair scatter plot of monthly rent against floor area for 90 apartments; rent rises with area, and Old Town is generally the most expensive neighborhood and Hillcrest the cheapest at a given size](https://www.pythoncharts.com/altair/altair-tutorial/images/altair-tutorial-quick-example.png)

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](https://vega.github.io/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](https://www.pythoncharts.com/altair/intro-to-altair/) |
| Draw bar and line charts | [Altair Bar and Line Charts](https://www.pythoncharts.com/altair/altair-bar-line-charts/) |
| Show details on hover | [Scatter Plot Tooltips](https://www.pythoncharts.com/altair/altair-scatter-plot-tooltips/) |
| Set axis titles and number formats | [Axis Titles and Formatting](https://www.pythoncharts.com/altair/altair-axis-titles-formatting/) |
| Overlay charts or place them side by side | [Layering and Concatenation](https://www.pythoncharts.com/altair/altair-layer-concat-charts/) |
| Make small multiples | [Facet Charts](https://www.pythoncharts.com/altair/altair-facet-charts/) |
| Brush, click and filter | [Interactive Selections](https://www.pythoncharts.com/altair/altair-interactive-selections/) |
| Draw a map | [Choropleth Maps](https://www.pythoncharts.com/altair/altair-choropleth-maps/) |
| Restyle every chart at once | [Altair Themes](https://www.pythoncharts.com/altair/altair-themes/) |
| Export PNG, SVG or HTML | [Save Altair Charts](https://www.pythoncharts.com/altair/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](https://www.pythoncharts.com/altair/save-altair-charts/) |
| `ValueError` about a field's encoding type | Add a suffix such as `:Q` to a column made by a transform | [Computed fields](https://www.pythoncharts.com/altair/altair-scatter-plot-tooltips/) |
| `add_selection` or `selection_single` raise deprecation warnings | They are Altair 4 names; use `add_params` and `selection_point` | [Selections](https://www.pythoncharts.com/altair/altair-interactive-selections/) |
| `width="container"` doesn't resize a faceted chart | Give each panel a fixed width | [Facets](https://www.pythoncharts.com/altair/altair-facet-charts/) |
| Months sort alphabetically | Pass `sort=None` to keep the data order | [Axes](https://www.pythoncharts.com/altair/altair-axis-titles-formatting/) |

## 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](https://www.pythoncharts.com/plotly/plotly-tutorial/) has 3D charts and a larger set of ready-made chart types, and [Bokeh](https://www.pythoncharts.com/bokeh/bokeh-tutorial/) gives more control over drawing and streaming updates. The [interactive library comparison](https://www.pythoncharts.com/python/python-interactive-charting-libraries-compared/) puts them side by side.

## Similar Topics

- [Altair Bar Charts and Line Charts: The Basics](https://www.pythoncharts.com/altair/altair-bar-line-charts/)
- [Plotnine Tutorial: ggplot2 Style Charts in Python](https://www.pythoncharts.com/plotnine/plotnine-tutorial/)
- [Charts in Streamlit](https://www.pythoncharts.com/streamlit/streamlit-charts-compared/)
