How do I make a single legend for many 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
When several subplots share the same series labels, repeating the legend in every axis wastes space and makes the figure harder to read. In Matplotlib, the standard solution is to collect the legend handles once and attach a single legend to the figure rather than to each subplot.
The Figure-Level Legend Pattern
The cleanest approach is fig.legend(...). Plot the lines on the individual axes, get the handles and labels from one axis or from all axes, and then place one shared legend on the figure.
Two lines matter most here:
- '
fig.legend(...)creates the shared legend.' - '
tight_layout(rect=[...])leaves room so the legend does not overlap the axes.'
Collecting Handles From Multiple Axes
If different subplots contain different plotted elements, collect handles and labels from all of them and deduplicate the labels.
This avoids duplicate entries such as repeated sales lines.
Common Legend Placements
A shared legend is often placed:
- above the subplots with
loc="upper center" - below the subplots with
loc="lower center" - outside the plotting area using
bbox_to_anchor
For example, to place the legend outside on the right:
This is useful when the labels are long and horizontal space is limited.
Using constrained_layout
If you prefer not to manage the layout rectangle manually, constrained_layout=True can help.
It can reduce layout friction, but figure-level legends still sometimes need manual positioning. The most reliable approach is to place the legend intentionally and reserve space for it.
What Not to Do
A common anti-pattern is calling ax.legend() on every subplot and then trying to hide three of them later. That technically works, but it creates clutter and makes layout tuning harder.
Another weak approach is building the shared legend from only one axis when different axes contain different labels. If the content varies, gather handles from all subplots instead.
Common Pitfalls
The biggest mistake is forgetting that fig.legend needs handles and labels. If you never capture or retrieve them, the shared legend has nothing to show.
Another issue is placing the legend correctly but forgetting to reserve figure space. The result is a legend that overlaps titles or gets cut off in the saved image.
Duplicate labels are also common when multiple axes plot the same named series. Deduplicating before calling fig.legend keeps the legend compact.
Finally, make sure all intended plotted objects actually have labels. Matplotlib ignores unlabeled artists or names beginning with an underscore.
Summary
- Use
fig.legend(...)for one legend shared across many subplots. - Get handles and labels from one axis when all subplots share the same series.
- Collect and deduplicate handles from all axes when subplot contents differ.
- Reserve space with
tight_layout(rect=[...])or careful figure placement. - Prefer one clear figure-level legend over repeated axis-level legends.
Related reading
- How do I make a U-matrix?
- How do I plot a classification graph of a SVM in R
- How do I plot in real-time in a while loop?
- How do I predict new data's cluster after clustering training data?
- How do I print the full NumPy array, without truncation?
- How do I print the full NumPy array, without truncation?
- How do I read a large csv file with pandas?
- How do I read CSV data into a record array in NumPy?
.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.