是否可以一次在 Altair 中显示一个多面图表并在不同图表之间切换?
Is it possible to display one faceted chart in Altair at a time and toggle between different charts?
我正在为一个 class 项目工作,我一直在 Altair 中创建多面散点图,其中每个图表都是不同类型的食物。我想知道是否仍然可以使用这些多面图表,但一次只显示一个图表,并让用户能够在图表之间切换,而不是让每个图表一个接一个地显示?
Here is a rough diagram of what I'm trying to do and here's the code I have now (and what the output looks like at the moment):
import altair as alt
from vega_datasets import data
hover = alt.selection_single(on='mouseover', nearest=True, empty='none')
base = alt.Chart("Food Nutrition Info Compiled.csv").encode(
x='Energ_Kcal:N',
y='Water_(g):Q',
color=alt.condition(hover, 'Type:N', alt.value('lightgray'))
).properties(
width=180,
height=180,
)
points = base.mark_point().add_selection(
hover
)
text = base.mark_text(dy=-5).encode(
text = 'Shrt_Desc:N',
opacity = alt.condition(hover, alt.value(1), alt.value(0))
)
alt.layer(points, text).facet(
'Type:N',
)
谢谢,我希望这是有道理的!
一次显示一个多面图表听起来与在任何一点显示数据的单个子集一样。这目前可以通过创建一个绑定到例如的选择来实现。一个下拉菜单并将其与 transform_filter
:
一起使用
import altair as alt
from vega_datasets import data
source = data.cars()
dropdown_options = source['Origin'].unique().tolist()
dropdown = alt.binding_select(
options=dropdown_options,
name='Origin '
)
selection = alt.selection_multi(
fields=['Origin'],
value=[{'Origin': dropdown_options[0]}],
bind=dropdown
)
alt.Chart(source).mark_circle().encode(
x=alt.X('Weight_in_lbs:Q', title=''),
y='Horsepower',
color='Origin',
).add_selection(
selection
).transform_filter(
selection
)
我正在为一个 class 项目工作,我一直在 Altair 中创建多面散点图,其中每个图表都是不同类型的食物。我想知道是否仍然可以使用这些多面图表,但一次只显示一个图表,并让用户能够在图表之间切换,而不是让每个图表一个接一个地显示? Here is a rough diagram of what I'm trying to do and here's the code I have now (and what the output looks like at the moment):
import altair as alt
from vega_datasets import data
hover = alt.selection_single(on='mouseover', nearest=True, empty='none')
base = alt.Chart("Food Nutrition Info Compiled.csv").encode(
x='Energ_Kcal:N',
y='Water_(g):Q',
color=alt.condition(hover, 'Type:N', alt.value('lightgray'))
).properties(
width=180,
height=180,
)
points = base.mark_point().add_selection(
hover
)
text = base.mark_text(dy=-5).encode(
text = 'Shrt_Desc:N',
opacity = alt.condition(hover, alt.value(1), alt.value(0))
)
alt.layer(points, text).facet(
'Type:N',
)
谢谢,我希望这是有道理的!
一次显示一个多面图表听起来与在任何一点显示数据的单个子集一样。这目前可以通过创建一个绑定到例如的选择来实现。一个下拉菜单并将其与 transform_filter
:
import altair as alt
from vega_datasets import data
source = data.cars()
dropdown_options = source['Origin'].unique().tolist()
dropdown = alt.binding_select(
options=dropdown_options,
name='Origin '
)
selection = alt.selection_multi(
fields=['Origin'],
value=[{'Origin': dropdown_options[0]}],
bind=dropdown
)
alt.Chart(source).mark_circle().encode(
x=alt.X('Weight_in_lbs:Q', title=''),
y='Horsepower',
color='Origin',
).add_selection(
selection
).transform_filter(
selection
)