matplotlib
pyplot
legend
fontsize
data visualization

How to change legend fontsize with matplotlib.pyplot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing the legend font size in Matplotlib is usually as simple as passing fontsize to legend(). The more interesting part is knowing when to use direct legend arguments, when to use prop, and when to change rcParams so your style applies across multiple plots instead of just one figure.

Core Sections

The direct answer: use fontsize in legend()

For one plot, the normal solution is to set the legend size directly.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y1 = [1, 4, 9, 16]
5y2 = [1, 2, 3, 4]
6
7plt.plot(x, y1, label="quadratic")
8plt.plot(x, y2, label="linear")
9plt.legend(fontsize=14)
10plt.show()

You can pass an integer, a float, or a named size such as "small" or "large".

This is the most readable choice when you only need to style one legend.

Use prop when you want more than size

If you need to control weight, family, or style as well as size, prop is the better argument.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4plt.plot(x, [1, 4, 9, 16], label="quadratic")
5plt.plot(x, [1, 2, 3, 4], label="linear")
6
7plt.legend(prop={"size": 13, "weight": "bold"})
8plt.show()

This is useful when the legend should match a broader typography style rather than only having larger text.

A practical rule is:

  • use fontsize when only the size matters
  • use prop when you want to style multiple font properties together

Change the legend after creation when needed

Sometimes you create the legend first and then adjust it afterward. That is fine too.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [1, 4, 9], label="data")
5legend = ax.legend()
6
7for text in legend.get_texts():
8    text.set_fontsize(12)
9
10plt.show()

This style is helpful when the legend is created inside a plotting helper and you only decide the final appearance later.

Use rcParams for project-wide defaults

If many plots in the same script or notebook should use the same legend font size, set it globally.

python
1import matplotlib.pyplot as plt
2
3plt.rcParams["legend.fontsize"] = 12
4
5plt.plot([1, 2, 3], [1, 4, 9], label="quadratic")
6plt.plot([1, 2, 3], [1, 2, 3], label="linear")
7plt.legend()
8plt.show()

This is especially useful in notebooks, dashboards, and reporting scripts where you want style consistency without repeating the same legend arguments everywhere.

Do not forget the legend title

The legend title has its own size control. If you set only fontsize, the entries change but the title may not.

python
1import matplotlib.pyplot as plt
2
3plt.plot([1, 2, 3], [1, 4, 9], label="quadratic")
4plt.plot([1, 2, 3], [1, 2, 3], label="linear")
5plt.legend(title="Models", fontsize=12, title_fontsize=14)
6plt.show()

That makes the title and entry text look intentional rather than accidentally mismatched.

Layout can break when text grows

When legend text becomes larger, the legend can overlap the plot or extend outside the figure. If that happens, the font setting is not wrong; the layout just needs adjustment.

Useful responses include:

  • moving the legend with loc
  • using bbox_to_anchor
  • calling tight_layout()
  • increasing figure size

Legend styling and figure layout often need to be tuned together.

Common Pitfalls

  • Setting fontsize and prop={"size": ...} at the same time creates ambiguity because prop controls the actual font properties.
  • Changing rcParams after a figure is already built does not retroactively restyle the existing legend.
  • Forgetting title_fontsize leaves the legend title visually inconsistent with the entries.
  • Enlarging legend text without adjusting layout can cause overlap or clipping.
  • Treating named sizes such as "large" as absolute values ignores that they are relative to the broader Matplotlib font configuration.

Summary

  • For one-off plots, plt.legend(fontsize=...) is the simplest solution.
  • Use prop when you want to control more than just size.
  • Use rcParams when you want a consistent legend font size across many plots.
  • Style the legend title separately if it should differ from the entry text.
  • If larger fonts make the figure look crowded, fix the layout rather than undoing the font change.

Course illustration
Course illustration

All Rights Reserved.