pandas Altair返回单个选择器错误

juud5qan  于 2023-05-27  发布在  其他
关注(0)|答案(1)|浏览(134)

尝试在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

更新

liwlm1x9

liwlm1x91#

我对你的代码做了如下修改:

  • selection_single()中删除了属性-它说在release notesselection_singleselection_multi现在已弃用;使用selection_point代替。
  • 删除了调用interactive()函数后添加的代码。

修改代码:

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

结果:

在Vega编辑器中打开图表

相关问题