redsunzire
redsunzire

Reputation: 55

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?

Upvotes: 4

Views: 3222

Answers (1)

jakevdp
jakevdp

Reputation: 86443

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')
)

Upvotes: 7

Related Questions