Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/).
699 changes: 699 additions & 0 deletions altair-python/altair-python.ipynb

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions altair-python/scatter_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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",
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.

This dropna() filters on columns that aren’t used in this chart (e.g. IMDB Rating, Major Genre, MPAA Rating), which changes which points appear in the basic scatter plot. Consider dropping NAs only for the fields you actually encode here, or add a short comment explaining why you’re pre-filtering extra columns for consistency with later examples.

Suggested change
"IMDB Rating",
"Major Genre",
"MPAA Rating",

Copilot uses AI. Check for mistakes.
]
)

scatter = (
alt.Chart(movies)
.mark_point()
.encode(
x="Production Budget:Q",
y="Worldwide Gross:Q",
)
)
scatter
44 changes: 44 additions & 0 deletions altair-python/scatter_connected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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",
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.

This dropna() filters on "MPAA Rating", but this script doesn’t use that field in any encoding. That will remove additional rows and can change the resulting chart. Consider limiting the subset to only the fields used in this script (or add a comment explaining the broader filtering choice).

Suggested change
"MPAA Rating",

Copilot uses AI. Check for mistakes.
]
)

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
26 changes: 26 additions & 0 deletions altair-python/scatter_encoding_channels copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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"],
)
)
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