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()
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=Trueorsharey=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:
This still works, but the figure-level label methods are clearer when available.
Pair common labels with shared axes
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:
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.
Related reading
- How to set env variable in Jupyter notebook
- How to set shape in placeholder for non-deterministic array size
- How to set the axis limits in Matplotlib?
- How to set the subplot axis range
- How to set environment variables in Python?
- How to set optuna's study.optimize verbosity to 0?
- How to set weights in Keras with a numpy array?
- How to set xlim and ylim for a subplot
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.