Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a label to Seaborn Heatmap color bar?

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?

like image 445
ishido Avatar asked Feb 07 '17 14:02

ishido


People also ask

How do you put a title on a heatmap?

Add Custom Title and Labels to Heatmap Add a title to the heatmap in red. title = addTitle(hmo,'Gene Expression Data','Color','red'); Change the title font size.

How do you set the color on a heatmap?

You can customize the colors in your heatmap with the cmap parameter of the heatmap() function in seaborn. The following examples show the appearences of different sequential color palettes.

How do I change my color palette in Seaborn heatmap?

You can change the color of the seaborn heatmap by using the color map using the cmap attribute of the heatmap.


2 Answers

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

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.

like image 129
miradulo Avatar answered Sep 18 '22 09:09

miradulo


You can use:

ax = sns.heatmap(data.pivot_table(index='y', columns='x', values='z')) ax.collections[0].colorbar.set_label("Hello") 
like image 23
kezzos Avatar answered Sep 22 '22 09:09

kezzos