Skip to content

Commit ee5e331

Browse files
authored
Merge pull request #754 from realpython/altair-python
Sample code for the article on Altair
2 parents 5ba5b5b + 791a866 commit ee5e331

File tree

7 files changed

+869
-0
lines changed

7 files changed

+869
-0
lines changed

altair-python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Altair: Declarative Charts With Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Altair: Declarative Charts With Python](https://realpython.com/altair-python/).

altair-python/altair-python.ipynb

Lines changed: 732 additions & 0 deletions
Large diffs are not rendered by default.

altair-python/scatter_basic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import altair as alt
2+
from altair.datasets import data
3+
4+
movies = data.movies()
5+
movies = movies.dropna(
6+
subset=[
7+
"Production Budget",
8+
"Worldwide Gross",
9+
]
10+
)
11+
12+
scatter = (
13+
alt.Chart(movies)
14+
.mark_point()
15+
.encode(
16+
x="Production Budget:Q",
17+
y="Worldwide Gross:Q",
18+
)
19+
)
20+
scatter

altair-python/scatter_connected.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import altair as alt
2+
from altair.datasets import data
3+
4+
movies = data.movies()
5+
movies = movies.dropna(
6+
subset=[
7+
"Production Budget",
8+
"Worldwide Gross",
9+
"IMDB Rating",
10+
"Major Genre",
11+
]
12+
)
13+
14+
brush = alt.selection_interval()
15+
16+
scatter = (
17+
alt.Chart(movies)
18+
.mark_point()
19+
.encode(
20+
x="Production Budget:Q",
21+
y="Worldwide Gross:Q",
22+
color=(
23+
alt.when(brush)
24+
.then("Major Genre:N")
25+
.otherwise(alt.value("lightgray")),
26+
),
27+
)
28+
.add_params(brush)
29+
)
30+
31+
scatter
32+
33+
bars = (
34+
alt.Chart(movies)
35+
.mark_bar()
36+
.encode(
37+
x="mean(IMDB Rating):Q",
38+
y="Major Genre:N",
39+
)
40+
.transform_filter(brush)
41+
)
42+
43+
scatter & bars
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import altair as alt
2+
from altair.datasets import data
3+
4+
movies = data.movies()
5+
movies = movies.dropna(
6+
subset=[
7+
"Production Budget",
8+
"Worldwide Gross",
9+
"IMDB Rating",
10+
"Major Genre",
11+
]
12+
)
13+
14+
scatter = (
15+
alt.Chart(movies)
16+
.mark_point()
17+
.encode(
18+
x="Production Budget:Q",
19+
y="Worldwide Gross:Q",
20+
color="Major Genre:N",
21+
size="IMDB Rating:Q",
22+
tooltip=["Title:N", "IMDB Rating:Q"],
23+
)
24+
)
25+
scatter

altair-python/scatter_faceted.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import altair as alt
2+
from altair.datasets import data
3+
4+
movies = data.movies()
5+
movies = movies.dropna(
6+
subset=[
7+
"Production Budget",
8+
"Worldwide Gross",
9+
"IMDB Rating",
10+
"Major Genre",
11+
"MPAA Rating",
12+
]
13+
)
14+
15+
scatter = (
16+
alt.Chart(movies)
17+
.mark_point()
18+
.encode(
19+
x="Production Budget:Q",
20+
y="Worldwide Gross:Q",
21+
color="Major Genre:N",
22+
size="IMDB Rating:Q",
23+
tooltip=["Title:N", "IMDB Rating:Q"],
24+
column="MPAA Rating:O",
25+
)
26+
)
27+
scatter

altair-python/steps.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import altair as alt
2+
import pandas as pd
3+
4+
steps = pd.DataFrame(
5+
{
6+
"Day": ["1-Mon", "2-Tue", "3-Wed", "4-Thu", "5-Fri", "6-Sat", "7-Sun"],
7+
"Steps": [6200, 8400, 7100, 9800, 5500, 9870, 3769],
8+
}
9+
)
10+
11+
weekly_steps = (
12+
alt.Chart(steps)
13+
.mark_bar()
14+
.encode(
15+
x="Day",
16+
y="Steps",
17+
)
18+
)
19+
weekly_steps

0 commit comments

Comments
 (0)