How to change font properties of matplotlib colorbar label?

In matplotlib, I want to change the font properties for the colorbar label. For example, I want the label to be in bold.

Here is a sample code:

 from matplotlib.pylab import * pcolor(arange(20).reshape(4,5)) cb = colorbar(label='a label') 

and the result where I want the label to be in bold:

output plot

All other answers on this site respond only to changing ticklabels or changing all fonts in general (by changing the matplotlibrc file)

+6
source share
4 answers

This two-line layer can be used with any Text property ( http://matplotlib.org/api/text_api.html#matplotlib.text.Text )

 cb = plt.colorbar() cb.set_label(label='a label',weight='bold') 
+6
source

As an alternative to unutbu's answer, you can take advantage of the fact that the color bar is another example of the axes in the figure and sets the label font as if you set any y-label.

 from matplotlib.pylab import * from numpy import arange pcolor(arange(20).reshape(4,5)) cb = colorbar(label='a label') ax = cb.ax text = ax.yaxis.label font = matplotlib.font_manager.FontProperties(family='times new roman', style='italic', size=16) text.set_font_properties(font) show() 

figure with colorbar

+12
source

I agree with francesco, but even shorten to one line:

 plt.colorbar().set_label(label='a label',size=15,weight='bold') 
+8
source

Maybe use TeX: r'\textbf{a label}'

 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt mpl.rc('text', usetex=True) plt.pcolor(np.arange(20).reshape(4,5)) cb = plt.colorbar(label=r'\textbf{a label}') plt.show() 

enter image description here

+2
source

All Articles