Altair: Format multi tooltip

Created on 12 Jun 2018  路  7Comments  路  Source: altair-viz/altair

Hello,
I cannot find a way to provide a format for each field of a multi tooltip. Here is a reproducible example :

import pandas as pd
import numpy as np
import altair as alt

np.random.seed(14)
data = pd.DataFrame({"date": pd.date_range("2018-01-01", periods=100),
                     "values": np.random.normal(size=100)})

alt.Chart(data).mark_point().encode(
    x="date:T",
    y="values:Q",
    tooltip=alt.Tooltip(['date:T', 'values:Q'], format='.2f')
)

Which formats also the date field:
screenshot from 2018-06-12 18-06-09
If I try something like

alt.Tooltip(['date:T', 'values:Q'], format=['%d/%m/%Y', '.2f'])

I have an error saying that format is not a string.

So, how can I specify the format of each field in this multi tooltip ?

question

Most helpful comment

You can set the encoding to a list of alt.Tooltip objects, each with their own format string:

import pandas as pd
import numpy as np
import altair as alt

np.random.seed(14)
data = pd.DataFrame({"date": pd.date_range("2018-01-01", periods=100),
                     "values": np.random.normal(size=100)})

alt.Chart(data).mark_point().encode(
    x="date:T",
    y="values:Q",
    tooltip=[alt.Tooltip('values:Q', format='.2f'),
             alt.Tooltip('date:T', format='%A, %B %e')]
)

screen shot 2018-06-12 at 9 16 48 am

All 7 comments

You can set the encoding to a list of alt.Tooltip objects, each with their own format string:

import pandas as pd
import numpy as np
import altair as alt

np.random.seed(14)
data = pd.DataFrame({"date": pd.date_range("2018-01-01", periods=100),
                     "values": np.random.normal(size=100)})

alt.Chart(data).mark_point().encode(
    x="date:T",
    y="values:Q",
    tooltip=[alt.Tooltip('values:Q', format='.2f'),
             alt.Tooltip('date:T', format='%A, %B %e')]
)

screen shot 2018-06-12 at 9 16 48 am

Oh, great, thanks!

Is there any way to add a percent symbol AND round using format= ?

From the example above:

tooltip=[alt.Tooltip('values:Q', format='.2f')

we tried

tooltip=[alt.Tooltip('values:Q', format='{.2f}%')

but there was a javascript error.

Any ideas ?

Format strings must follow d3-format; so you probably want something like format='.2%'

Ah! That's it - thanks!

Just for additional information, if you need to show DateTime format in your tooltip, you could refer to this d3-time-format

I wonder if I am the only one who does the same formatting. Ex for y-axis as well as for tooltip.

Is there any feature to just use the same formatting? cc @jakevdp

Sorry for pushing the old issue 馃檲

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dzonimn picture dzonimn  路  3Comments

morberg picture morberg  路  3Comments

bmcfee picture bmcfee  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments

nielsmde picture nielsmde  路  4Comments