thengineer
thengineer

Reputation: 595

How to change font properties of a matplotlib colorbar label?

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

Here is some example code:

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

and the result, where I want "a label" to appear bold:

output plot

All other answers on this site only answer how to change ticklabels or change all fonts in general (via modification of the matplotlibrc file)

Upvotes: 28

Views: 104072

Answers (6)

hamflow
hamflow

Reputation: 319

Just remember some of the attributes of colorbar objects are for the main object and some of them are attributes of label module:

plt.colorbar(shrink = 1, orientation='horizontal').set_label(label='Label Example',size=15)

Upvotes: 0

ASE
ASE

Reputation: 1870

To change the font size of your colorbar's tick and label:

clb=plt.colorbar()
clb.ax.tick_params(labelsize=8) 
clb.ax.set_title('Your Label',fontsize=8)

This can be also used if you have sublots:

plt.tight_layout()
plt.subplots_adjust(bottom=0.05)
cax = plt.axes([0.1, 0, 0.8, 0.01]) #Left,bottom, length, width
clb=plt.colorbar(cax=cax,orientation='horizontal')
clb.ax.tick_params(labelsize=8) 
clb.ax.set_title('Your Label',fontsize=8)

Upvotes: 4

Q-man
Q-man

Reputation: 2219

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

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

Upvotes: 25

francesco
francesco

Reputation: 570

This two-liner 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')

Upvotes: 27

Molly
Molly

Reputation: 13610

As an alternative to unutbu's answer you could take advantage of the fact that a color bar is another axes instance in the figure and set the label font like you would 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

Upvotes: 19

unutbu
unutbu

Reputation: 881007

Perhaps 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

Upvotes: 3

Related Questions