堆叠密度估计#
要绘制堆叠估计图,请使用共享的 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)