How to add a label to Seaborn Heatmap color bar?

后端 未结 2 1292
猫巷女王i
猫巷女王i 2020-12-29 18:37

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(         


        
2条回答
  •  醉梦人生
    2020-12-29 19:26

    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'})
    

    It is worth noting that cbar_kws can be handy for setting other attributes on the colorbar such as tick frequency or formatting.

提交回复
热议问题