gpt4 book ai didi

python - Altair:使用 binding_select 时选择所有值

转载 作者:行者123 更新时间:2023-12-04 17:27:36 28 4
gpt4 key购买 nike

首次呈现图表时,binding_select() 没有/全部选中,因此显示所有值。使用下拉列表进行选择后,如何再次选择所有值?

在下面的示例中,首先显示所有数据点。然后进行选择,仅显示与该位置对应的数据点。然后如何返回到图表中显示所有数据点的原始状态。

source = pd.DataFrame({'Position':['Center','Center','Power Forward','Point Guard','Point Guard','Shooting Guard','Power Forward','Point Guard','Point Guard'],
'Points_Per_Game':[12 , 13, 15 , 21, 22, 9, 8, 5, 7],
'Games_Played':[22, 9, 8, 5, 7 , 12 , 13, 15 , 21]})

position_dropdown = alt.binding_select(options=list(source.Position.unique()))
position_selection = alt.selection_single(fields=['Position'], bind=position_dropdown, name='Player ')

alt.Chart(source).mark_circle(size=100).encode(
x='Games_Played:N',
y='Points_Per_Game:N',

).properties(selection=interval).add_selection(
position_selection,
).transform_filter(
position_selection
)

最佳答案

您需要将选项 None 添加到下拉选择中,以便在未选择任何内容时调用默认行为。您可以设置一个标签,上面写着“全部”,以便清楚地了解该选项的作用:

source = pd.DataFrame({'Position':['Center','Center','Power Forward','Point Guard','Point Guard','Shooting Guard','Power Forward','Point Guard','Point Guard'],
'Points_Per_Game':[12 , 13, 15 , 21, 22, 9, 8, 5, 7],
'Games_Played':[22, 9, 8, 5, 7 , 12 , 13, 15 , 21]})

position_dropdown = alt.binding_select(
options=[None] + list(source.Position.unique()), labels = ['All'] + list(source.Position.unique()))
position_selection = alt.selection_single(fields=['Position'], bind=position_dropdown, name='Player ')

alt.Chart(source).mark_circle(size=100).encode(
x='Games_Played:N',
y='Points_Per_Game:N',
).add_selection(
position_selection,
).transform_filter(
position_selection
)

enter image description here

关于python - Altair:使用 binding_select 时选择所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62315591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com