Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altair data aggregation

I am trying to plot on Altair the following: x = daily time stamp y = count of records as the data is nominal not quantitative

when i try to aggregate x by month or quarters, is it possible to show y as the average count of records per day instead of the the sum?

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('Date:T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

when i try to do

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('month(Date):T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

it gets aggregated by total for the month, is it possible to do average of the count per month?

like image 769
redsunzire Avatar asked Oct 14 '25 04:10

redsunzire


1 Answers

You can apply multiple aggregates using Altair's transform syntax. For your chart, you might do something like this:

alt.Chart(data).transform_aggregate(
    daily_count = 'count(Name)',
    groupby=['Date', 'descriptor']
).mark_bar().encode(
    x = alt.X('month(Date):O'),
    y = alt.Y('mean(daily_count):Q'),
    color = alt.Color('descriptor:N')
)
like image 189
jakevdp Avatar answered Oct 16 '25 17:10

jakevdp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!