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:

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 ?
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')]
)

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 馃檲
Most helpful comment
You can set the encoding to a list of
alt.Tooltipobjects, each with their own format string: