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 %'}
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With