Normalized Stacked Bar Chart

< Horizontal Stacked Bar Chart | Example Gallery | Stacked Bar Chart >

from altair import *

Chart('https://vega.github.io/vega-lite/data/population.json').mark_bar(
    stacked='normalize',
).encode(
    color=Color('gender:N',
        scale=Scale(
            range=['#EA98D2', '#659CCA'],
        ),
    ),
    x=X('age:O',
        scale=Scale(
            bandSize=17.0,
        ),
    ),
    y=Y('sum(people):Q',
        axis=Axis(
            title='population',
        ),
    ),
).transform_data(
    calculate=[Formula(
            expr='datum.sex == 2 ? "Female" : "Male"',
            field='gender',
        )],
    filter='datum.year == 2000',
)