matplotlib
python
data visualization
subplots
axes labels

How to set common axis labels for Matplotlib subplots (supxlabel)

Codemia Knowledge Hub ·

Call fig.supxlabel() and fig.supylabel() on the figure. They add one x-axis label and one y-axis label for the whole subplot grid, so you do not repeat the same label on every subplot.

fig.supxlabel() and fig.supylabel()

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4x = np.linspace(0, 10, 100)
5
6fig, axes = plt.subplots(2, 2, sharex=True, sharey=True, constrained_layout=True)
7
8for ax in axes.flat:
9    ax.plot(x, np.sin(x))
10
11fig.supxlabel("Time (s)")
12fig.supylabel("Amplitude")
13
14plt.show()

Repeating the same set_xlabel() and set_ylabel() text on every subplot clutters the figure. A figure-level label communicates the same information once and frees up visual space for the data, while still making the shared axes obvious. It helps most when:

  • The subplots share the same units
  • You are using sharex=True or sharey=True
  • The figure has several rows or columns

Only use common labels when the subplots really do share the same axis meaning.

Fallback for older Matplotlib: fig.text()

If your Matplotlib version does not support supxlabel and supylabel, place text on the figure canvas yourself:

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4x = np.linspace(0, 10, 100)
5fig, axes = plt.subplots(2, 2)
6
7for ax in axes.flat:
8    ax.plot(x, np.cos(x))
9
10fig.text(0.5, 0.04, "Time (s)", ha="center")
11fig.text(0.04, 0.5, "Amplitude", va="center", rotation="vertical")
12
13plt.show()

This still works, but the figure-level label methods are clearer when available.

Pair common labels with shared axes

python
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)

Sharing axes tells Matplotlib the subplot scales line up, which makes a single label correct both visually and semantically.

Clear per-subplot labels

Common labels work best when the inner subplots do not all repeat their own labels. In many figures you can leave the per-axis labels off and rely on the figure-wide ones:

python
for ax in axes.flat:
    ax.set_xlabel("")
    ax.set_ylabel("")

That small cleanup step is often what makes the final figure feel intentional instead of crowded.

Leave room with layout management

Figure-level labels still need space. Without layout management such as constrained_layout=True or plt.tight_layout(), they can overlap other elements. constrained_layout=True is often the easiest fix; manual spacing helps when titles, legends or colorbars compete for room.

If the figure also has a suptitle, check the final render carefully. The title, shared labels and subplot spacing all compete for canvas space, and small layout adjustments make a noticeable difference.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design