I've created a visualisation of some data using the Altair library in Python. Currently the hover infobox displays the median. How do I change the aggregation to display mean?
If you're talking about a standard boxplot as in the example at https://altair-viz.github.io/gallery/boxplot.html , there is no easy way to change the median to the mean. This is because the median is hard-coded in the vega-lite boxplot macro on which this is based: https://vega.github.io/vega-lite/docs/boxplot.html
If you want more flexibility, it is possible to construct the chart components manually and use the mean rather than median; for example:
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'),
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.