Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions altair-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Altair: Declarative Charts With Python

This folder provides the code examples for the Real Python tutorial [Altair: Declarative Charts With Python](https://realpython.com/altair-python/).
732 changes: 732 additions & 0 deletions altair-python/altair-python.ipynb

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions altair-python/scatter_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import altair as alt
from altair.datasets import data

movies = data.movies()
movies = movies.dropna(
subset=[
"Production Budget",
"Worldwide Gross",
]
)

scatter = (
alt.Chart(movies)
.mark_point()
.encode(
x="Production Budget:Q",
y="Worldwide Gross:Q",
)
)
scatter
43 changes: 43 additions & 0 deletions altair-python/scatter_connected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import altair as alt
from altair.datasets import data

movies = data.movies()
movies = movies.dropna(
subset=[
"Production Budget",
"Worldwide Gross",
"IMDB Rating",
"Major Genre",
]
)

brush = alt.selection_interval()

scatter = (
alt.Chart(movies)
.mark_point()
.encode(
x="Production Budget:Q",
y="Worldwide Gross:Q",
color=(
alt.when(brush)
.then("Major Genre:N")
.otherwise(alt.value("lightgray")),
),
Comment on lines +22 to +26
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra parentheses around the color= expression are unnecessary and make this already-long line harder to read. Consider removing the redundant parentheses and letting the formatter wrap the chained call for readability.

Suggested change
color=(
alt.when(brush)
.then("Major Genre:N")
.otherwise(alt.value("lightgray")),
),
color=alt.when(brush)
.then("Major Genre:N")
.otherwise(alt.value("lightgray")),

Copilot uses AI. Check for mistakes.
)
.add_params(brush)
)

scatter

bars = (
alt.Chart(movies)
.mark_bar()
.encode(
x="mean(IMDB Rating):Q",
y="Major Genre:N",
)
.transform_filter(brush)
)

scatter & bars
25 changes: 25 additions & 0 deletions altair-python/scatter_encoding_channels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import altair as alt
from altair.datasets import data
Comment on lines +1 to +2
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filename contains a space and the suffix "copy", which makes it awkward to import/execute from shells and looks like an accidental duplicate. Please rename this file to a conventional slug/snake-case name without spaces (and without "copy"), e.g. "scatter_encoding_channels.py".

Copilot uses AI. Check for mistakes.

movies = data.movies()
movies = movies.dropna(
subset=[
"Production Budget",
"Worldwide Gross",
"IMDB Rating",
"Major Genre",
]
)

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
27 changes: 27 additions & 0 deletions altair-python/scatter_faceted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import altair as alt
from altair.datasets import data

movies = data.movies()
movies = movies.dropna(
subset=[
"Production Budget",
"Worldwide Gross",
"IMDB Rating",
"Major Genre",
"MPAA Rating",
]
)

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
19 changes: 19 additions & 0 deletions altair-python/steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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",
Comment on lines +15 to +16
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the other examples in this folder, the encodings here don’t specify field types (e.g. Day:N, Steps:Q). Making the types explicit keeps the example consistent and avoids relying on Altair’s type inference if the data source changes.

Suggested change
x="Day",
y="Steps",
x="Day:N",
y="Steps:Q",

Copilot uses AI. Check for mistakes.
)
)
weekly_steps
Loading