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

标签 python altair

首次呈现图表时,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/

相关文章:

python - 获取副词和形容词对应的动词和名词

python - 属性错误 : Unknown property legend in seaborn

python - 使用 NumPy 时如何根据数据类型创建数据子集?

python - 在 Altair 图中绘制中位数和平均值

python - 如何在 python 中使用 altair 包加载和绘制 csv 文件?

python - 将宽变长但重复特定列

python - 将数组排序到索引数组指定的容器中的最有效方法?

altair - 如何在聚合工具提示中显示所有值?

python - 调整 Altair 分组条形图的标签

python - 如何使用 Python Altair 将 x 轴和 y 轴的交点从 0 移动到 1?