堆叠密度估计#
要绘制估计值的堆叠图,使用一个共享的 extent 和固定数量的细分 steps,以确保每个区域的点对齐良好。每种鸢尾花特征的测量密度估计以堆叠的方式绘制。此外,将 counts 设置为 true 会将密度乘以每组中的数据点数量,从而保持比例差异。
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
groupby=['Measurement_type'],
extent= [0, 8],
counts = True,
steps=200
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q').stack('zero'),
alt.Color('Measurement_type:N')
).properties(width=400, height=100)
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
groupby=['Measurement_type'],
extent= [0, 8],
counts = True,
steps=200
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q', stack='zero'),
alt.Color('Measurement_type:N')
).properties(width=400, height=100)