尝试在Altair中的Vega中创建一个滑块。
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)
不知道这个错误是什么意思。
altair.vegalite.v4.schema.core.SelectionDef->2->init, validating 'anyOf'
1955 is not valid under any of the given schemas
更新
1条答案
按热度按时间liwlm1x91#
我对你的代码做了如下修改:
selection_single()
中删除了属性-它说在release notes上selection_single
和selection_multi
现在已弃用;使用selection_point
代替。interactive()
函数后添加的代码。修改代码:
结果:
在Vega编辑器中打开图表