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.
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.
This is useful when the legend should match a broader typography style rather than only having larger text.
A practical rule is:
- use
fontsizewhen only the size matters - use
propwhen 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.
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.
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.
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
fontsizeandprop={"size": ...}at the same time creates ambiguity becausepropcontrols the actual font properties. - Changing
rcParamsafter a figure is already built does not retroactively restyle the existing legend. - Forgetting
title_fontsizeleaves 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
propwhen you want to control more than just size. - Use
rcParamswhen 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.

