Search code examples
altair

Altair: add numbers of observation per group


How can I add the numbers of observations per group to a boxplot with altair?

I am looking for something like this:

enter image description here

But I am also open to other solutions (as long as the information is visible).

For instance, how would I add this information to the example plot from here?

import altair as alt
from vega_datasets import data

source = data.population.url

alt.Chart(source).mark_boxplot(extent='min-max').encode(
    x='age:O',
    y='people:Q'
)

Thank you so much for your help!


Solution

  • You cannot dynamically modify titles in Altair/Vega-Lite, so you would need to add a text mark with this information. Something like this (just a proof of principal, you would likely add some explanatory text and make it prettier):

    import altair as alt
    from vega_datasets import data
    
    source = data.population.url
    
    boxes = alt.Chart(source).mark_boxplot(extent='min-max').encode(
        x='age:O',
        y='people:Q'
    )
    
    boxes + boxes.mark_text().encode(
        y=alt.datum(11_500_000),
        text='count()'
    )
    

    enter image description here

    Two other options are to make the sizes of the boxes correspond to the count or add it as the tooltip.