Search code examples
pythonaltair

Is there a way to format tooltip values in Altair boxplot


Is is possible to format the values within a tooltip for a boxplot? From this Vega documentation, it appears so, but I can't quite figure out how to do it with Altair for python

from vega_datasets import data

import altair as alt

source = data.population.url

alt.Chart(source).mark_boxplot().encode(
    alt.X("age:O"),
    alt.Y("people:Q"),
    tooltip=[
        alt.Tooltip("people:Q", format=",.2f"),
    ],
)

enter image description here


Solution

  • I believe you need to provide an aggregation for composite marks like mark_boxplot. This works:

    from vega_datasets import data
    import altair as alt
    
    source = data.population.url
    
    alt.Chart(source).mark_boxplot().encode(
        alt.X("age:O"),
        alt.Y("people:Q"),
        tooltip=alt.Tooltip("mean(people):Q", format=",.2f"),)
    

    enter image description here

    Update: As it is currently impossible to add multiple aggregated tooltips to a boxplot, I combined my answer with How to change Altair boxplot infobox to display mean rather than median? to put a transparent box with a custom tooltip on top of the boxplot. I still kept the boxplot underneath in order to have the outliers and whiskers plotted as a Tukey boxplot instead of min-max. I also added a point for the mean, since this is what I wanted to see in the tooltip:

    alt.Chart(source).mark_boxplot(median={'color': '#353535'}).encode(
        alt.X("age:O"),
        alt.Y("people:Q"),
        tooltip=[
            alt.Tooltip("people:Q", format=",.2f"),
        ],
    ) + alt.Chart(source).mark_circle(color='#353535', size=15).encode(
        x='age:O',
        y='mean(people):Q'
    ) + alt.Chart(source).transform_aggregate(
        min="min(people)",
        max="max(people)",
        mean="mean(people)",
        median="median(people)",
        q1="q1(people)",
        q3="q3(people)",
        count="count()",
        groupby=['age']
    ).mark_bar(opacity=0).encode(
        x='age:O',
        y='q1:Q',
        y2='q3:Q',
        tooltip=alt.Tooltip(['min:Q', 'q1:Q', 'mean:Q', 'median:Q', 'q3:Q', 'max:Q', 'count:Q'], format='.1f')
    )
    

    enter image description here