data visualization
plotting
legend positioning
chart customization
matplotlib

How to put the legend outside the plot

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Goal: How to put the legend outside the plot

Direct Answer

Set layout and typography explicitly in Matplotlib instead of relying on defaults.

  1. Reproduce the behavior with a minimal test case.
  2. Verify runtime, dependencies, and configuration.
  3. Apply the smallest targeted fix.
  4. Re-run the validation on real input.

Concrete Example

python
1import matplotlib.pyplot as plt
2fig, ax = plt.subplots(figsize=(10, 5))
3ax.plot([1,2,3], [1,4,9])
4ax.legend(["series"], loc="upper left", bbox_to_anchor=(1.02, 1))
5ax.tick_params(labelsize=10)
6fig.suptitle("Overview", fontsize=14)
7plt.tight_layout()
8plt.show()

Validation Checklist

  • Primary scenario behaves as expected.
  • Edge cases are explicitly handled.
  • The change is repeatable in the target environment.

Common Pitfalls

  • Verify versions and active configuration before changing code.
  • Use representative sample input instead of synthetic happy-path only.
  • Change one variable at a time so regressions are attributable.

Summary

Solve for correctness first, then optimize. Tags: data visualization, plotting, legend positioning, chart customization, matplotlib.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.