我正在尝试创建带有复选框的Altair图表.默认情况下,该复选框处于未选中状态,但我希望选中该默认复选框. HTML复选框具有“ checked”属性来实现此目的,但我不知道如何通过Altair API传达此信息.这可能吗?
在下面的示例中,“ A”和“ B”系列开始着色,并且未选中复选框.期望的行为是要选中这些框,而当未选中它们时,相应的系列将显示为灰色.
data = pd.DataFrame(
{
"Block":("A","A","B","A"),"X":(10,15,11,30,45,9),"Y":(1.1,10,3.5,5,20),}
)
a_data = data.loc[data["Block"] == "A"]
b_data = data.loc[data["Block"] == "B"]
a_filter = alt.binding_checkBox()
b_filter = alt.binding_checkBox()
a_select = alt.selection_single(
fields=["Block"],bind=a_filter,name="A"
)
b_select = alt.selection_single(
fields=["Block"],bind=b_filter,name="B"
)
a_cond = alt.condition(
a_select,alt.Color("Block:N"),alt.value("lightgray")
)
b_cond = alt.condition(
b_select,alt.value("lightgray")
)
a_chart = alt.Chart(a_data).mark_point(filled=True).encode(
x="X:Q",y=alt.Y(
"Y:Q",scale=alt.Scale(type="log")
),color=a_cond
).add_selection(a_select)
b_chart = alt.Chart(b_data).mark_point(filled=True).encode(
x="X:Q",color=b_cond
).add_selection(b_select)
(a_chart + b_chart)
最佳答案