legend - Altair - 交互式图例仅显示选定的元素

标签 legend interactive altair

我有以下代码

brush = alt.selection_interval()
selection = alt.selection_multi(fields=['Sex'])
color = alt.condition(selection,
                      alt.Color('Sex:N', legend=None),
                      alt.value('white'))

scatter = alt.Chart(abaloneData).mark_circle().encode(
    alt.X(alt.repeat("column"), type='quantitative'),
    alt.Y(alt.repeat("row"), type='quantitative'),
    color=color
).properties(
    width=140,
    height=140
).repeat(
    row= ['Ring Number', 'Diameter',  'Shell Length', 'Height'],
    column=['Whole Wt', 'Shucked Wt', 'Viscera Wt', 'Shell Wt']
    #try switching these to see which wa looks better 
).add_selection(
    brush
)
legend = alt.Chart(abaloneData).mark_point().encode(
    y=alt.Y('Sex:N', axis=alt.Axis(orient='right')),
    color=color
).add_selection(
    selection
)
scatter | legend

但是我想找到一种方法,当用户选择按图例中的性别进行过滤时,使图中的其他元素消失。有什么办法可以做到这一点吗?我知道有一种方法可以使用单选按钮和下拉过滤器来完成此操作,但我只是想知道是否也可以使用图例来完成此操作。

谢谢

最佳答案

您可以使用 native 交互式图例并调节所选内容的不透明度。像这样的东西(未经测试,因为您没有提供任何示例数据):

selection = alt.selection_multi(fields=['Sex'], bind='legend')

scatter = alt.Chart(abaloneData).mark_circle().encode(
    alt.X(alt.repeat("column"), type='quantitative'),
    alt.Y(alt.repeat("row"), type='quantitative'),
    color=alt.Color('Sex:N'),
    opacity=alt.condition(selection, alt.value(1.0), alt.value(0.0))
).properties(
    width=140,
    height=140
).repeat(
    row= ['Ring Number', 'Diameter',  'Shell Length', 'Height'],
    column=['Whole Wt', 'Shucked Wt', 'Viscera Wt', 'Shell Wt']
    #try switching these to see which wa looks better 
).add_selection(
    selection
)

关于legend - Altair - 交互式图例仅显示选定的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64645464/

相关文章:

bash - 为半可移植 shell 脚本制作弹出菜单的最佳(?)方法?

jquery - HTML5 中的交互式图像

python - Altair条形图具有可变宽度的条形?

python - Altair 中的 slider 标签

r - 如何在 ggplot2 中为使用 stat_summary 制作的线条添加图例?

javascript - 创建带有幻灯片动画的 HTML5 交互式幻灯片

python - 是否可以通过多次绘图函数调用迭代地建立图例条目?

python-3.x - Vega-Lite 的某些投影在 Altair 中不起作用

d3.js - NVD3 : How to get values from tooltip or legend instead of labels?

python - 没有标签的python中的图例