如何在Seaborn Heatmap颜色栏中添加标签?
问题描述:
如果我有以下数据和Seaborn热图:
If I have the following data and Seaborn Heatmap:
import pandas as pd
data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(14,15,23,2)})
sns.heatmap(data.pivot_table(index='y', columns='x', values='z'))
如何在颜色栏中添加标签?
How do I add a label to the colour bar?
答
您可以在从ax
收集它之后进行设置,也可以像这样简单地在cbar_kws
中传递标签.
You could set it afterwards after collecting it from an ax
, or simply pass a label in cbar_kws
like so.
import seaborn as sns
import pandas as pd
data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(14,15,23,2)})
sns.heatmap(data.pivot_table(index='y', columns='x', values='z'),
cbar_kws={'label': 'colorbar title'})
值得注意的是,cbar_kws
可以方便地在颜色栏上设置其他属性,例如刻度频率或格式.
It is worth noting that cbar_kws
can be handy for setting other attributes on the colorbar such as tick frequency or formatting.