重命名 Altair 中的工具提示

Man*_*r G 6 python tooltip altair

我正在尝试使用以下代码在 Altair 中绘制图表。

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)']
)
Run Code Online (Sandbox Code Playgroud)

但由于工具提示名称很奇怪,我想使用“as”在图表上重命名它,如下所示。

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]
)
Run Code Online (Sandbox Code Playgroud)

如何重命名工具提示,使其以更易读的方式显示给查看图表的用户。

jak*_*vdp 8

您可以title通过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')]
)
Run Code Online (Sandbox Code Playgroud)


Rag*_*rok 6

作为 jakevdp 答案的附录,如果有人遇到这个问题并且需要为两个不同的功能做一个替代标题(就像我对地图所做的那样),代码的“tooltip =”部分将像这样工作:

tooltip=[alt.Tooltip('properties.feature1:O', title="Feature 1"), alt.Tooltip('properties.feature2:Q', title="Feature 2")]
Run Code Online (Sandbox Code Playgroud)