Trying to create a slider on the gapminder dataset from vega in Altair.
import pandas as pd
import altair as alt
from vega_datasets import data
df= data.gapminder()
yearslider = alt.selection_single(
name="Year",
field="year",
init={"year": 1955},
bind=alt.binding_range(min=1955, max=2005, step=5)
)
alt.Chart(df).mark_circle().encode(
alt.X("fertility:Q", title=None, scale=alt.Scale(zero=False)),
alt.Y("life_expect:Q", title="Life expectancy",
scale=alt.Scale(zero=False)),
alt.Size("pop:Q", scale=alt.Scale(range=[0, 1000]),
legend=alt.Legend(orient="bottom")),
alt.Color("cluster:N", legend=None),
alt.Shape("cluster:N"),
alt.Order("pop:Q", sort="descending"),
tooltip=['country:N'],
).configure_view(fill="#fff").properties(title="Number of children by mother").interactive().add_selector(yearslider).transform_filter(yearslider)
Don't know what the error means.
altair.vegalite.v4.schema.core.SelectionDef->2->init, validating 'anyOf'
1955 is not valid under any of the given schemas
Updated
I applied the following modifications to your code:
selection_single()
- it says on the release notes selection_single
and selection_multi
are now deprecated; use selection_point
instead.interactive()
function.Modified code:
yearslider = alt.selection_single()
alt.Chart(df).mark_circle().encode(
alt.X("fertility:Q", title=None, scale=alt.Scale(zero=False)),
alt.Y("life_expect:Q", title="Life expectancy",
scale=alt.Scale(zero=False)),
alt.Size("pop:Q", scale=alt.Scale(range=[0, 1000]),
legend=alt.Legend(orient="bottom")),
alt.Color("cluster:N", legend=None),
alt.Shape("cluster:N"),
alt.Order("pop:Q", sort="descending"),
tooltip=['country:N'],
).configure_view(fill="#fff").properties(title="Number of children by mother").interactive()
Result: