-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Sample code for the article on Altair #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/). |
Large diffs are not rendered by default.
| 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", | ||
| ] | ||
| ) | ||
|
|
||
| scatter = ( | ||
| alt.Chart(movies) | ||
| .mark_point() | ||
| .encode( | ||
| x="Production Budget:Q", | ||
| y="Worldwide Gross:Q", | ||
| ) | ||
| ) | ||
| scatter | ||
| 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", | ||||||||||||||||||
|
||||||||||||||||||
| "MPAA Rating", |
Copilot
AI
Apr 9, 2026
There was a problem hiding this comment.
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.
| color=( | |
| alt.when(brush) | |
| .then("Major Genre:N") | |
| .otherwise(alt.value("lightgray")), | |
| ), | |
| color=alt.when(brush) | |
| .then("Major Genre:N") | |
| .otherwise(alt.value("lightgray")), |
| 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 |
| 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 |
| 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
|
||||||||||
| x="Day", | |
| y="Steps", | |
| x="Day:N", | |
| y="Steps:Q", |
There was a problem hiding this comment.
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.