There’s a moment many data analysts know well: you have a new dataset and a clear question, and you open a notebook only to find yourself writing boilerplate axis and figure setup before you’ve even looked at the data. Matplotlib gives you fine-grained control, but that control comes with a cost. Altair takes a completely different approach to data visualization in Python.
Instead of scripting every visual detail, you describe what your data means. This includes specifying which column goes on which axis, what should be colored, and what should be interactive. Altair then generates the visualization.
If you’re wondering whether it’s worth adding another visualization library to your toolkit, here’s how Altair and Matplotlib compare:
| Use Case | Pick Altair | Pick Matplotlib |
|---|---|---|
| Interactive exploratory charts in notebooks | ✅ | — |
| Pixel-precise publication figures or 3D plots | — | ✅ |
Altair generates web-native charts. The output is HTML and JavaScript, which means charts render right in your notebook and can be saved as standalone HTML files or embedded in web pages. It’s not a replacement for Matplotlib, and it doesn’t try to be. Think of them as tools you reach for in different situations.
Get Your Code: Click here to download the free sample code you’ll use to build interactive Python charts the declarative way with Altair.
Take the Quiz: Test your knowledge with our interactive “Altair: Declarative Charts With Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Altair: Declarative Charts With PythonTest your knowledge of Altair, the declarative data visualization library for Python that turns DataFrames into interactive charts.
Start Using Altair in Python
It’s a good idea to install Altair in a dedicated virtual environment. It pulls in several dependencies like pandas and the Vega-Lite renderer, and a virtual environment keeps them from interfering with your other projects. Create one and install Altair with pip:
$ python -m venv altair-venv
$ source altair-venv/bin/activate
(altair-venv) $ python -m pip install altair
This tutorial uses Python 3.14 and Altair 6.0. All the code runs inside a Jupyter notebook, which is the most common environment for interactive data exploration with Altair. If you prefer a different JavaScript-capable environment like VS Code, Google Colab, or JupyterLab, feel free to use that instead. To launch a Jupyter notebook, run the following:
(altair-venv) $ python -m pip install notebook
(altair-venv) $ jupyter notebook
The second command launches the Jupyter Notebook server in your browser. Create a new notebook and enter the following code, which builds a bar chart from a small DataFrame containing daily step counts for one week:
import altair as alt
import pandas as pd
steps = pd.DataFrame({
"Day": ["1-Mon", "2-Tue", "3-Wed", "4-Thu", "5-Fri", "6-Sat", "7-Sun"],
"Steps": [6200, 8400, 7100, 9800, 5500, 9870, 3769],
})
weekly_steps = alt.Chart(steps).mark_bar().encode(
x="Day",
y="Steps",
)
weekly_steps
You should see a bar chart displaying daily step counts:

The dataset is intentionally minimal because data isn’t the main focus: it has seven rows for seven days, and two columns for the day name and step count. Notice how the weekly_steps chart is constructed. Every Altair chart follows this same pattern. It’s built from these three building blocks:
- Data: A pandas DataFrame handed to
alt.Chart(). - Mark: The visual shape you want, chosen via
.mark_*(). Here,.mark_bar()draws bars. Other options include.mark_point(),.mark_line(), and.mark_arc(). - Encode: The mapping from data columns to visual properties, declared inside
.encode(). Here,Daygoes to the x-axis andStepsto the y-axis.
This is Altair’s core grammar in action: Data → Mark → Encode. You’ll use it every time.



