Text of realpython.com › altair-python as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Altair: Declarative Charts With Python – Real Python

Altair: Declarative Charts With Python by Valerio Maggio Updated Apr 22, 2026 Reading time estimate 14m intermediate data-science data-viz Table of Contents Remove ads 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 Python Test 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 : Language: Shell $ 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: Language: Shell (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: Language: Python 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: Step Counts as a Bar Chart 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, Day goes to the x-axis and Steps to the y-axis. This is Altair’s core grammar in action: Data → Mark → Encode . You’ll use it every time. The key difference between Matplotlib and Altair lies in your approach to creating charts . Matplotlib is imperative , meaning you provide explicit instructions to create a figure, add an axes object, set labels, and adjust ticks. It’s a sequence of drawing commands.

Conversely, Altair is declarative . You describe the data and desired appearance, and Altair handles the rest. The end results are similar, but the underlying mental models are fundamentally different. Remove ads Explore Multiple Dimensions With Encoding Channels Now that you’re familiar with Altair’s pattern for creating charts, it’s time to apply it to a dataset with more to explore. Altair provides a collection of example datasets through the altair.datasets module.

Some are bundled with the package , while Altair fetches others, like movies , from the network on demand. You’ll use this dataset throughout the rest of this tutorial. This dataset is a good playground, with a mix of quantitative columns like Production Budget , Worldwide Gross , and IMDB Rating (Internet Movie Database), alongside categorical ones like Major Genre and MPAA Rating . It also contains several columns with missing values, like a true real-world dataset.

You can load the movies dataset and drop rows with missing values in the columns you’ll use in the examples ahead: Language: Python from altair.datasets import data movies = data . movies () movies = movies . dropna ( subset = [ "Production Budget" , "Worldwide Gross" , "IMDB Rating" , "Major Genre" , "MPAA Rating" , ]) With the cleaned dataset ready, you can start exploring it visually. The next idea to explore is encoding channels .

In Altair, an encoding channel is a mapping between a column in your data and a visual property of the chart, such as position, color, or size. You already used two encoding channels in weekly_steps without naming them explicitly: x and y . Start there and build a basic scatter plot with the movies data: Language: Python scatter = alt . Chart ( movies ) . mark_point () . encode ( x = "Production Budget:Q" , y = "Worldwide Gross:Q" , ) scatter This creates a basic scatter plot of worldwide earnings against production budgets.

The :Q suffix is a type shorthand that tells Altair how to interpret the column’s values. The following table shows the four available type shorthands: Shorthand Type Example :Q Quantitative (numeric) Production Budget:Q :N Nominal (unordered categories) Major Genre:N :O Ordinal (ordered categories) MPAA Rating:O :T Temporal (dates and times) Release Date:T Altair can often infer the type directly from the DataFrame, but being explicit avoids surprises.

You’ll see this shorthand everywhere in Altair documentation and examples online. Now start adding channels. Altair supports many more, including color , size , tooltip , opacity , shape , and column . Each new channel is just another argument to .encode() : Language: Python scatter = alt . Chart ( movies ) . mark_point () . encode ( x = "Production Budget:Q" , y = "Worldwide Gross:Q" , color = "Major Genre:N" , size = "IMDB Rating:Q" , tooltip = [ "Title:N" , "IMDB Rating:Q" ], ) scatter With three additional lines, the chart now visually represents genre with color and IMDb rating with point size: Scatter Plot With Color and Size Encoding Channels Hovering over any point reveals the film’s title and rating.

The legend and size scale appear automatically based on the column types that Altair infers. Now see what happens when you add one more channel to control the chart’s layout: Language: Python scatter = alt . Chart ( movies ) . mark_point () . encode ( x = "Production Budget:Q" , y = "Worldwide Gross:Q" , color = "Major Genre:N" , size = "IMDB Rating:Q" , tooltip = [ "Title:N" , "IMDB Rating:Q" ], column = "MPAA Rating:O" , ) scatter That single extra colu

The rest of this page is on the live site.