How to show a histogram of percentages instead of counts using Altair How to show a histogram of percentages instead of counts using Altair pandas pandas

How to show a histogram of percentages instead of counts using Altair


You can do this with a Join Aggregate transform followed by a Calculate transform:

import pandas as pdimport altair as altsource = pd.DataFrame({'age': ['12', '32', '43', '54', '32', '32', '12']})alt.Chart(source).transform_joinaggregate(    total='count(*)').transform_calculate(    pct='1 / datum.total').mark_bar().encode(    alt.X('age:Q', bin=True),    alt.Y('sum(pct):Q', axis=alt.Axis(format='%')))

enter image description here


Edit: this was my initial answer, which is much more complicated:

It's not entirely straightforward, because it requires manually specifying the bin and aggregate transforms currently implied by your encoding, followed by a calculate transform to compute the percentages. Here is an example:

import pandas as pdimport altair as altsource = pd.DataFrame({'age': ['12', '32', '43', '54', '32', '32', '12']})alt.Chart(source).transform_bin(    ['age_min', 'age_max'],    field='age',).transform_aggregate(    count='count()',    groupby=['age_min', 'age_max']).transform_joinaggregate(    total='sum(count)'  ).transform_calculate(    pct='datum.count / datum.total'  ).mark_bar().encode(    alt.X("age_min:Q", bin='binned'),    x2='age_max',    y=alt.Y('pct:Q', axis=alt.Axis(format='%')))

enter image description here

I'm hoping that we'll be able to streamline the transform API in the future.