-
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
base: master
Are you sure you want to change the base?
Changes from all 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,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 |
| 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")), | ||
| ), | ||
| ) | ||
| .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 | ||
| 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
|
||
|
|
||
| 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 | ||
| 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.
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.