How to add a label to Seaborn Heatmap color bar? How to add a label to Seaborn Heatmap color bar? python python

How to add a label to Seaborn Heatmap color bar?


You could set it afterwards after collecting it from an ax, or simply pass a label in cbar_kws like so.

import seaborn as snsimport pandas as pddata = 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'})

enter image description here

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


You can use:

ax = sns.heatmap(data.pivot_table(index='y', columns='x', values='z'))ax.collections[0].colorbar.set_label("Hello")