HOWTO · Matplotlib
How to Set the Figure Title and Axes Labels Font Size in Matplotlib
Set Matplotlib title and axis-label sizes with fontsize, rcParams defaults, or each text object's set_size() method.
On this page
For a single plot, pass fontsize to the title and label calls. To apply reusable defaults, update rcParams; to change existing text objects individually, call set_size(). The examples use an Axes title. A figure-level title is separate and is set with fig.suptitle().
Set title and axis-label sizes with fontsize
Pass fontsize directly to Axes.set_title(), Axes.set_xlabel(), and Axes.set_ylabel(). These calls control the title and labels for that Axes. If you also want a title above the whole figure, use fig.suptitle() and give it its own fontsize value.
Matplotlib expresses numeric font sizes in points. Use the same value on several calls for a consistent look, or choose a larger title size to establish a visual hierarchy. Because the value is passed to each text-setting call, one label can be adjusted without changing the others.
This complete example creates a plot, sets the Axes title and both labels, and adds a distinct figure-level title. Change the point sizes to fit your layout.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
ax.set_title("Values by x", fontsize=16)
ax.set_xlabel("x", fontsize=12)
ax.set_ylabel("value", fontsize=12)
fig.suptitle("Example figure", fontsize=20)
plt.show()
Set default font sizes with rcParams
rcParams stores Matplotlib’s configuration defaults. Updating it changes the defaults used by subsequently created plot elements in the current Python process. Set these values before making the plot when you want the defaults to apply consistently; pass fontsize on a particular call when that text needs a different size.
Use axes.labelsize for x- and y-axis labels, axes.titlesize for Axes titles, and figure.titlesize for figure-level titles made with fig.suptitle(). The first two settings are separate from the figure title setting. These parameters accept a size such as a number of points or a supported named size.
import matplotlib.pyplot as plt
plt.rcParams.update({
"axes.labelsize": 12,
"axes.titlesize": 16,
"figure.titlesize": 20,
})
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
ax.set_title("Values by x")
ax.set_xlabel("x")
ax.set_ylabel("value")
fig.suptitle("Example figure")
plt.show()
If only a particular plot needs different defaults, use matplotlib.rc_context() to apply temporary rcParams settings for a block of code. This keeps those choices from becoming the defaults for later plots in the process.
For example, with plt.rc_context({"axes.labelsize": 12, "axes.titlesize": 16}): scopes those two settings to the indented plotting code. Outside that block, Matplotlib restores the prior settings. This is handy in a script that creates plots with different styles.
Adjust individual title and label sizes with set_size()
Each title or axis label is a text object with a set_size() method. This is useful when a plot already exists or when the title and labels need different values. The following example gets the current Axes with plt.gca(), then sets the size on its title and label text objects.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
ax.set_title("Values by x")
ax.set_xlabel("x")
ax.set_ylabel("value")
axes = plt.gca()
axes.title.set_size(16)
axes.xaxis.label.set_size(12)
axes.yaxis.label.set_size(12)
plt.show()
When working with multiple Axes, keep a reference to the one you want to change, as in ax.title.set_size(16), rather than relying on plt.gca(), which returns whichever Axes is current. For a figure-level title, set its size when calling fig.suptitle(), or keep the returned text object and call set_size() on it. An Axes title belongs to one panel; the figure title describes the overall figure.