交互式平均值#
下面的图表使用了区间选择,这使得图表包含一个交互式画刷(显示为灰色)。画刷选择参数化了红色的参考线,该参考线可视化了所选区间内的平均值。
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
brush = alt.selection_interval(encodings=['x'])
bars = alt.Chart().mark_bar().encode(
x='month(date):O',
y='mean(precipitation):Q',
opacity = alt.when(brush).then(alt.value(1)).otherwise(alt.value(0.7)),
).add_params(
brush
)
line = alt.Chart().mark_rule(color='firebrick').encode(
y='mean(precipitation):Q',
size=alt.SizeValue(3)
).transform_filter(
brush
)
alt.layer(bars, line, data=source)
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
brush = alt.selection_interval(encodings=['x'])
bars = alt.Chart().mark_bar().encode(
x='month(date):O',
y='mean(precipitation):Q',
opacity = alt.when(brush).then(alt.value(1)).otherwise(alt.value(0.7)),
).add_params(
brush
)
line = alt.Chart().mark_rule(color='firebrick').encode(
y='mean(precipitation):Q',
size=alt.SizeValue(3)
).transform_filter(
brush
)
alt.layer(bars, line, data=source)
# No channel encoding options are specified in this chart
# so the code is the same as for the method-based syntax.