简体   繁体   中英

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:

输出图

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

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()

用colorbar图

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

我同意francesco,但即使缩短到一行:

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

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()

在此输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM