elk*_*000 3 python pandas altair
我使用 Python 中的 Altair 库创建了一些数据的可视化。当前悬停信息框显示中位数。如何更改聚合以显示平均值?
如果您正在讨论https://altair-viz.github.io/gallery/boxplot.html示例中的标准箱线图,则没有简单的方法可以将中位数更改为平均值。这是因为中位数是硬编码在其所基于的 vega-lite boxplot 宏中的: https: //vega.github.io/vega-lite/docs/boxplot.html
如果您想要更大的灵活性,可以手动构建图表组件并使用平均值而不是中位数;例如:
import altair as alt
from vega_datasets import data
source = data.population.url
alt.LayerChart(data=source).transform_aggregate(
min="min(people)",
max="max(people)",
mean="mean(people)",
q1="q1(people)",
q3="q3(people)",
groupby=["age"]
).encode(
x='age:O',
tooltip=['min:Q', 'q1:Q', 'mean:Q', 'q3:Q', 'max:Q']
).add_layers(
alt.Chart().mark_rule().encode(y='min:Q', y2='max:Q'),
alt.Chart().mark_bar(width=15).encode(y='q1:Q', y2='q3:Q'),
alt.Chart().mark_tick(color='white', width=15).encode(y='mean:Q'),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1520 次 |
| 最近记录: |