matplotlib
python
data visualization
subplots
axes labels

How to set common axes labels for subplots

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When several Matplotlib subplots share the same meaning for the x-axis or y-axis, repeating labels on every subplot creates clutter. The cleanest modern solution is to set figure-level labels with fig.supxlabel() and fig.supylabel().

Use Figure-Level Labels

Matplotlib provides figure-wide axis labels for exactly this situation.

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

This keeps the subplot grid clean while still making the shared axes obvious.

Why This Is Better Than Repeating Labels

If every subplot repeats the same set_xlabel() and set_ylabel() text, the figure becomes harder to read. Common labels communicate the same information once at the figure level and free up visual space for the data.

This is especially helpful when:

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

Fallback for Older Matplotlib Versions

If you are on an older Matplotlib version that does not support supxlabel and supylabel, you can place text manually on the figure canvas.

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 older approach is still valid, but figure-level label methods are clearer when available.

Use Shared Axes Thoughtfully

Common labels pair naturally with shared axes:

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

That tells Matplotlib the subplot scales align, which makes common labeling more natural both visually and semantically.

Remove Redundant Axis Labels on Individual Plots

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

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.

Layout Still Matters

Even with figure-level labels, spacing can still be an issue. constrained_layout=True is often the easiest fix, but plt.tight_layout() or manual spacing can also help when a figure has titles, legends, or colorbars competing for room.

If the figure also has a suptitle, inspect the final render carefully. The title, shared labels, and subplot spacing all compete for canvas space, so small layout adjustments can make a noticeable difference in readability. Figure-level labels help, but they still need room to breathe. That final check is worth doing every time. A clean figure usually comes from several small layout choices working together. Common labels are one of those choices, not the whole story.

Common Pitfalls

  • Figure-level labels work best when the subplots really do share the same axis meaning.
  • Without layout management such as constrained_layout=True or tight_layout(), labels can overlap other figure elements.
  • Repeating axis labels on every subplot defeats the point of common labels.
  • For older Matplotlib versions, use fig.text() as a fallback instead of assuming supxlabel exists.

Summary

  • Use fig.supxlabel() and fig.supylabel() for common axes labels in modern Matplotlib.
  • Combine them with shared axes when the subplots represent the same units and scales.
  • Use fig.text() as a fallback on older Matplotlib versions.
  • Figure-level labels usually make multi-plot figures cleaner and easier to read.

Course illustration
Course illustration

All Rights Reserved.