Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename tooltip in altair

I am trying to plot a chart in Altair using the below code.

tot_matches_played = alt.Chart(mpt).mark_bar().encode(
  alt.X('Team',axis=alt.Axis(title='Teams Played in IPL'), sort=alt.EncodingSortField(field='Number_of_matches_played:Q', op='count', order='ascending')),  
  alt.Y('Number_of_matches_played:Q' ),
  tooltip=['sum(Number_of_matches_played)']
)

But since the tooltip name is weird, I would like to rename it on the chart using "as", something like below.

tot_matches_played = alt.Chart(mpt).mark_bar().encode(
  alt.X('Team',axis=alt.Axis(title='Teams Played in IPL'), sort=alt.EncodingSortField(field='Number_of_matches_played:Q', op='count', order='ascending')),  
  alt.Y('Number_of_matches_played:Q' ),
  tooltip=['sum(Number_of_matches_played)' as total_matches]
)

How to rename a tooltip so that it would appear in a more readable way to users looking at the chart.

like image 212
Manoj Kumar G Avatar asked Sep 07 '18 13:09

Manoj Kumar G


2 Answers

You can adjust the title output via alt.Tooltip:

tot_matches_played = alt.Chart(mpt).mark_bar().encode(
  alt.X('Team',axis=alt.Axis(title='Teams Played in IPL'), sort=alt.EncodingSortField(field='Number_of_matches_played:Q', op='count', order='ascending')),  
  alt.Y('Number_of_matches_played:Q' ),
  tooltip=[alt.Tooltip('sum(Number_of_matches_played)', title='matches played')]
)
like image 139
jakevdp Avatar answered Nov 25 '22 19:11

jakevdp


As an addendum to jakevdp's answer, if anyone comes across this and needs to do an alternative title for two different features (as I did for a map), the "tooltip=" part of the code will work like this:

tooltip=[alt.Tooltip('properties.feature1:O', title="Feature 1"), alt.Tooltip('properties.feature2:Q', title="Feature 2")]
like image 33
Ragnar Lothbrok Avatar answered Nov 25 '22 18:11

Ragnar Lothbrok