Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to format tooltip values in Altair boxplot

Tags:

python

altair

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

like image 889
Brent Gunderson Avatar asked Mar 13 '21 15:03

Brent Gunderson


2 Answers

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

like image 64
joelostblom Avatar answered Sep 30 '22 11:09

joelostblom


There is a way to add multiple columns to the tooltip. You can pass in multiple columns in square brackets as a list.

import altair as alt
from vega_datasets import data

stocks = data.stocks()

alt.Chart(stocks).mark_point().transform_window(
    previous_price = 'lag(price)'
).transform_calculate(
    pct_change = '(datum.price - datum.previous_price) / datum.previous_price'
).encode(
    x='date',
    y='price',
    color='symbol',
    tooltip=[ 'price', 'symbol', alt.Tooltip('pct_change:Q', format='.1%')]
)
like image 34
Haq Nawaz Avatar answered Sep 30 '22 11:09

Haq Nawaz