Altair - Interactive Legend 仅显示选定元素
Altair - Interactive Legend to only show selected elements
我有以下代码
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
但我想找到一种方法,当用户选择按性别从图例中过滤时,我情节中的其他元素就会消失。有什么办法可以做到这一点?我知道有一种方法可以使用单选按钮和下拉过滤器来完成,但我只是想知道是否也可以使用图例来完成。
谢谢
您可以使用本机交互式图例并调整选区的不透明度。像这样的东西(未经测试,因为您没有提供任何示例数据):
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
)
我有以下代码
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
但我想找到一种方法,当用户选择按性别从图例中过滤时,我情节中的其他元素就会消失。有什么办法可以做到这一点?我知道有一种方法可以使用单选按钮和下拉过滤器来完成,但我只是想知道是否也可以使用图例来完成。
谢谢
您可以使用本机交互式图例并调整选区的不透明度。像这样的东西(未经测试,因为您没有提供任何示例数据):
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
)