Skip to content
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

Tidy up U.S. Population by Age and Sex case study #2628

Merged
merged 2 commits into from
Jul 2, 2022
Merged
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
32 changes: 17 additions & 15 deletions altair/examples/us_population_over_time.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
US Population Over Time
=======================
US Population by Age and Sex
============================
This chart visualizes the age distribution of the US population over time.
It uses a slider widget that is bound to the year to visualize the age
distribution over time.
Expand All @@ -11,21 +11,23 @@

source = data.population.url

pink_blue = alt.Scale(domain=('Male', 'Female'),
range=["steelblue", "salmon"])

slider = alt.binding_range(min=1900, max=2000, step=10)
select_year = alt.selection_single(name="year", fields=['year'],
bind=slider, init={'year': 2000})
select_year = alt.selection_single(
name="Year",
fields=["year"],
bind=alt.binding_range(min=1900, max=2000, step=10, name="Year"),
init={"year": 2000},
)

alt.Chart(source).mark_bar().encode(
x=alt.X('sex:N', title=None),
y=alt.Y('people:Q', scale=alt.Scale(domain=(0, 12000000))),
color=alt.Color('sex:N', scale=pink_blue),
column='age:O'
).properties(
width=20
).add_selection(
x=alt.X("sex:N", axis=alt.Axis(labels=False, title=None, ticks=False)),
y=alt.Y("people:Q", scale=alt.Scale(domain=(0, 12000000)), title="Population"),
color=alt.Color(
"sex:N",
scale=alt.Scale(domain=("Male", "Female"), range=["steelblue", "salmon"]),
title="Sex",
),
column=alt.Column("age:O", title="Age"),
).properties(width=20, title="U.S. Population by Age and Sex").add_selection(
select_year
).transform_calculate(
"sex", alt.expr.if_(alt.datum.sex == 1, "Male", "Female")
Expand Down