How to set a single, main title above all the subplots
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In Matplotlib, a title for the entire figure should usually be attached to the Figure, not to any individual Axes. The standard tool for that is fig.suptitle(), which creates one title above all subplots.
The main complication is layout. Once you add a figure-level title, you often need to adjust tight_layout, constrained_layout, or subplot spacing so the title does not overlap the axes.
Use suptitle on the Figure
This places one title above the whole grid, not inside any single subplot.
That distinction matters because figure-level layout and axes-level layout are managed separately in Matplotlib.
Why ax.set_title() Is Not the Right Tool
ax.set_title() applies only to one subplot:
That is useful for panel-specific labels, but it is not a replacement for a global title. If you want both, combine them:
- '
fig.suptitle()for the whole figure' - '
ax.set_title()for each subplot'
Avoid Overlap with tight_layout
The common pitfall is that the figure title overlaps the top row of subplots when tight_layout() compresses everything.
One fix is to leave room explicitly:
The rect argument reserves the top part of the figure for the title.
Use constrained_layout in Newer Code
Another option is to let Matplotlib manage spacing more automatically:
constrained_layout is often easier than manually tuning subplot spacing, especially when labels, legends, and titles all compete for room.
It is especially helpful in notebooks and quick analysis scripts where you want a decent default layout without manually tweaking subplot margins every time.
A Useful Pattern with Individual Titles
This creates a clear hierarchy:
- figure title for the overall meaning
- subplot titles for local meaning
That visual hierarchy is usually what people actually want when they ask for one main title above multiple subplot panels.
It also keeps figure-level meaning separate from panel-level meaning, which makes multi-plot reports much easier to read.
That separation is a small detail, but it improves readability immediately in dashboards, notebooks, and exported reports.
Common Pitfalls
- Using
ax.set_title()and expecting it to become a figure-wide title. - Calling
tight_layout()without leaving room forsuptitle(). - Adding the title after saving or rendering the figure and wondering why it is missing.
- Mixing manual spacing tweaks and
constrained_layoutwithout understanding which layout system is active. - Making the figure title too large for the available top margin.
Summary
- Use
fig.suptitle()for one title above all subplots. - Use
ax.set_title()only for subplot-specific titles. - Reserve room with
tight_layout(rect=...)if overlap occurs. - '
constrained_layout=Trueis often the easiest modern layout option.' - Figure-wide and subplot-specific titles can be used together when spaced correctly.
Related reading
- how to set camera position for 3d plots using python/matplotlib?
- How to set class_weight in keras package of R?
- How to set common axes labels for subplots
- 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 weights in Keras with a numpy array?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.