How to add a title to each subplot
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, each subplot in a figure can have its own title set with ax.set_title('Title'). The figure itself can have an overall title set with fig.suptitle('Main Title'). When using plt.subplots() to create a grid of axes, iterate over the axes array and call set_title() on each. Proper spacing with plt.tight_layout() or fig.subplots_adjust() prevents titles from overlapping with neighboring subplots.
Basic Subplot Titles
set_title() places a title centered above each subplot.
Figure Title + Subplot Titles
fig.suptitle() creates a title for the entire figure. The rect parameter in tight_layout() reserves space at the top.
Customizing Title Appearance
Title Alignment
The loc parameter accepts 'left', 'center' (default), or 'right'.
Dynamic Titles in Loops
Using axes.flat converts a 2D array of axes into a 1D iterator, making it easy to loop over all subplots.
Titles with plt.subplot (Older API)
plt.title() sets the title for the current subplot when using the stateful pyplot interface.
Seaborn Subplot Titles
Seaborn builds on Matplotlib, so the same set_title() method works:
FacetGrid Titles
Seaborn's FacetGrid and catplot set titles automatically. Customize them with set_titles():
Preventing Title Overlap
tight_layout() automatically adjusts spacing. For finer control, use subplots_adjust(hspace=..., wspace=...) where hspace is vertical spacing and wspace is horizontal spacing.
Common Pitfalls
- Titles overlapping with adjacent subplots: Without
plt.tight_layout()orconstrained_layout=True, subplot titles can overlap with the axes of neighboring rows. Always calltight_layout()beforeshow(). - Using
plt.title()instead ofax.set_title():plt.title()only works on the current active subplot. When working with the object-oriented API (fig, axes = plt.subplots()), useax.set_title()to avoid setting the title on the wrong subplot. suptitlehidden behind subplot titles: The figure-levelsuptitlecan overlap with the top row of subplot titles. Useplt.tight_layout(rect=[0, 0, 1, 0.95])to reserve space at the top.- Iterating over
axeswhen it's 2D:plt.subplots(2, 3)returns a 2D array. Useaxes.flatto iterate over all axes in a single loop, oraxes.flatten()to get a 1D array. plt.subplots(1, 1)returns a single Axes, not an array: With a single subplot,axesis anAxesobject, not an array. Usefig, axes = plt.subplots(1, 1)and accessaxesdirectly, or usesqueeze=Falseto always get an array.
Summary
- Use
ax.set_title('Title')to add a title to each individual subplot - Use
fig.suptitle('Title')for an overall figure title - Customize titles with
fontsize,fontweight,color,loc, andpadparameters - Use
axes.flatto iterate over all subplots when setting titles in a loop - Call
plt.tight_layout()to prevent titles from overlapping with adjacent subplots
Related reading
- How to add an empty column to a dataframe?
- How to add conda environment to jupyter lab
- How to add dummies to Pandas DataFrame?
- How to add header row to a pandas DataFrame
- How to add an integer to each element in a list?
- How to add hours to current time in python
- How to add hovering annotations to a plot
- How to add multiple columns to pandas dataframe in one assignment
.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.