Why is altair single select not changing my graph?

Viewed 202

I've tried to google why this isn't working but it doesn't appear obvious to me. Can you please tell me why the selection dropdown isn't changing the graph? Thanks

source = data.unemployment_across_industries.url
df = data.unemployment_across_industries()
series_val = df.series.str.split(',').explode().unique().tolist()
print(series_val)
input_dropdown = alt.binding_select(options=series_val, name='Industry: ')
selection = alt.selection_single(fields=['series'], bind=input_dropdown)

fig3 = alt.Chart(source).mark_area().encode(
    alt.X('yearmonth(date):T',
        axis=alt.Axis(format='%Y', domain=False, tickSize=0)
    ),
    alt.Y('sum(count):Q', stack='center', axis=None),
    alt.Color('series:N',
        scale=alt.Scale(scheme='category20b')
    )
).add_selection(
    selection
)
fig3
1 Answers

The selection doesn't change the graph because you have not conditioned any part of the graph on the selection (none of your encodings depend on the value of the selection, nor is the data filtered based on the selection).

Perhaps you intended to do something like this?

fig3 = alt.Chart(source).mark_area().encode(
    alt.X('yearmonth(date):T',
        axis=alt.Axis(format='%Y', domain=False, tickSize=0)
    ),
    alt.Y('sum(count):Q', stack='center', axis=None),
    color=alt.condition(selection,
            alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
            alt.value('lightgray'))
).add_selection(
    selection
)

For more on how to use selections in Altair charts, see https://altair-viz.github.io/user_guide/interactions.html

Related