How do I set the figure title and axes labels font size in Matplotlib? How do I set the figure title and axes labels font size in Matplotlib? python python

How do I set the figure title and axes labels font size in Matplotlib?


Functions dealing with text like label, title, etc. accept parameters same as matplotlib.text.Text. For the font size you can use size/fontsize:

from matplotlib import pyplot as plt    fig = plt.figure()plt.plot(data)fig.suptitle('test title', fontsize=20)plt.xlabel('xlabel', fontsize=18)plt.ylabel('ylabel', fontsize=16)fig.savefig('test.jpg')

For globally setting title and label sizes, mpl.rcParams contains axes.titlesize and axes.labelsize. (From the page):

axes.titlesize      : large   # fontsize of the axes titleaxes.labelsize      : medium  # fontsize of the x any y labels

(As far as I can see, there is no way to set x and y label sizes separately.)

And I see that axes.titlesize does not affect suptitle. I guess, you need to set that manually.


You can also do this globally via a rcParams dictionary:

import matplotlib.pylab as pylabparams = {'legend.fontsize': 'x-large',          'figure.figsize': (15, 5),         'axes.labelsize': 'x-large',         'axes.titlesize':'x-large',         'xtick.labelsize':'x-large',         'ytick.labelsize':'x-large'}pylab.rcParams.update(params)


If you're more used to using ax objects to do your plotting, you might find the ax.xaxis.label.set_size() easier to remember, or at least easier to find using tab in an ipython terminal. It seems to need a redraw operation after to see the effect. For example:

import matplotlib.pyplot as plt# set up a plot with dummy datafig, ax = plt.subplots()x = [0, 1, 2]y = [0, 3, 9]ax.plot(x,y)# title and labels, setting initial sizesfig.suptitle('test title', fontsize=12)ax.set_xlabel('xlabel', fontsize=10)ax.set_ylabel('ylabel', fontsize='medium')   # relative to plt.rcParams['font.size']# setting label sizes after creationax.xaxis.label.set_size(20)plt.draw()

I don't know of a similar way to set the suptitle size after it's created.


matomo