Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn heatmap - colorbar label font size

How do I set the font size of the colorbar label?

ax=sns.heatmap(table, vmin=60, vmax=100, xticklabels=[4,8,16,32,64,128],yticklabels=[2,4,6,8], cmap="PuBu",linewidths=.0, 
        annot=True,cbar_kws={'label': 'Accuracy %'}

enter image description here

like image 844
Alessandro Gaballo Avatar asked Feb 02 '18 16:02

Alessandro Gaballo


Video Answer


1 Answers

Unfortunately seaborn does not give access to the objects it creates. So one needs to take the detour, using the fact that the colorbar is an axes in the current figure and that it is the last one created, hence

ax = sns.heatmap(...)
cbar_axes = ax.figure.axes[-1]

For this axes, we may set the fontsize by getting the ylabel using its set_size method.

Example, setting the fontsize to 20 points:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns
data = np.random.rand(10, 12)*100
ax = sns.heatmap(data, cbar_kws={'label': 'Accuracy %'})
ax.figure.axes[-1].yaxis.label.set_size(20)

plt.show()

enter image description here

Note that the same can of course be achieved by via

ax = sns.heatmap(data)
ax.figure.axes[-1].set_ylabel('Accuracy %', size=20)

without the keyword argument passing.

like image 154
ImportanceOfBeingErnest Avatar answered Sep 20 '22 13:09

ImportanceOfBeingErnest