# category: case studies
import altair as alt
from vega_datasets import data

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})

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(
    select_year
).transform_calculate(
    "sex", alt.expr.if_(alt.datum.sex == 1, "Male", "Female")
).transform_filter(
    select_year
).configure_facet(
    spacing=8
)

# category: case studies
import altair as alt
from vega_datasets import data

source = data.population.url

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", 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")
).transform_filter(
    select_year
).configure_facet(
    spacing=8
)

Tidy up U.S. Population by Age and Sex
Tidy up U.S. Population by Age and Sex
  # category: case studies
  import altair as alt
  from vega_datasets import data
  
  source = data.population.url
  
- pink_blue = alt.Scale(domain=('Male', 'Female'),
-                       range=["steelblue", "salmon"])
- 
+ select_year = alt.selection_single(
+     name="Year",
+     fields=["year"],
- slider = alt.binding_range(min=1900, max=2000, step=10)
+     bind=alt.binding_range(min=1900, max=2000, step=10, name="Year"),
- select_year = alt.selection_single(name="year", fields=['year'],
-                                    bind=slider, init={'year': 2000})
+     init={"year": 2000},
+ )
  
  alt.Chart(source).mark_bar().encode(
-     x=alt.X('sex:N', title=None),
+     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))),
+     y=alt.Y("people:Q", scale=alt.Scale(domain=(0, 12000000)), title="Population"),
-     color=alt.Color('sex:N', scale=pink_blue),
-     column='age:O'
- ).properties(
-     width=20
- ).add_selection(
+     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")
  ).transform_filter(
      select_year
  ).configure_facet(
      spacing=8
  )
  
Tidy up U.S. Population by Age and Sex
## Code Before:
# category: case studies
import altair as alt
from vega_datasets import data

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})

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(
    select_year
).transform_calculate(
    "sex", alt.expr.if_(alt.datum.sex == 1, "Male", "Female")
).transform_filter(
    select_year
).configure_facet(
    spacing=8
)

## Instruction:
Tidy up U.S. Population by Age and Sex
## Code After:
# category: case studies
import altair as alt
from vega_datasets import data

source = data.population.url

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", 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")
).transform_filter(
    select_year
).configure_facet(
    spacing=8
)
