Per the official guide, use of pylab
is no longer recommended. matplotlib.pyplot
should be used directly instead.
Globally setting font sizes via rcParams
should be done with
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 16
plt.rcParams['axes.titlesize'] = 16
# or
params = {'axes.labelsize': 16,
'axes.titlesize': 16}
plt.rcParams.update(params)
# or
import matplotlib as mpl
mpl.rc('axes', labelsize=16, titlesize=16)
# or
axes = {'labelsize': 16,
'titlesize': 16}
mpl.rc('axes', **axes)
The defaults can be restored using
plt.rcParams.update(plt.rcParamsDefault)
You can also do this by creating a style sheet in the stylelib
directory under the matplotlib configuration directory (you can get your configuration directory from matplotlib.get_configdir()
). The style sheet format is
axes.labelsize: 16
axes.titlesize: 16
If you have a style sheet at /path/to/mpl_configdir/stylelib/mystyle.mplstyle
then you can use it via
plt.style.use('mystyle')
# or, for a single section
with plt.style.context('mystyle'):
# ...
You can also create (or modify) a matplotlibrc file which shares the format
axes.labelsize = 16
axes.titlesize = 16
Depending on which matplotlibrc file you modify these changes will be used for only the current working directory, for all working directories which do not have a matplotlibrc file, or for all working directories which do not have a matplotlibrc file and where no other matplotlibrc file has been specified. See this section of the customizing matplotlib page for more details.
A complete list of the rcParams
keys can be retrieved via plt.rcParams.keys()
, but for adjusting font sizes you have (italics quoted from here)
axes.labelsize
- Fontsize of the x and y labels
axes.titlesize
- Fontsize of the axes title
figure.titlesize
- Size of the figure title (Figure.suptitle()
)
xtick.labelsize
- Fontsize of the tick labels
ytick.labelsize
- Fontsize of the tick labels
legend.fontsize
- Fontsize for legends (plt.legend()
, fig.legend()
)
legend.title_fontsize
- Fontsize for legend titles, None
sets to the same as the default axes. See this answer for usage example.
all of which accept string sizes {'xx-small', 'x-small', 'smaller', 'small', 'medium', 'large', 'larger', 'x-large', 'xxlarge'}
or a float
in pt
. The string sizes are defined relative to the default font size which is specified by
font.size
- the default font size for text, given in pts. 10 pt is the standard value
Additionally, the weight can be specified (though only for the default it appears) by
font.weight
- The default weight of the font used by text.Text
. Accepts {100, 200, 300, 400, 500, 600, 700, 800, 900}
or 'normal'
(400), 'bold'
(700), 'lighter'
, and 'bolder'
(relative with respect to current weight).