Pass fontsize= directly to plt.legend(fontsize=14) (or ax.legend(fontsize=14)) for an individual plot—along with title_fontsize=16 if the legend has a title—or configure plt.rcParams["legend.fontsize"] = 14 (and plt.rcParams["legend.title_fontsize"] = 16) globally at the top of your script to apply the size across every figure automatically.
Basic Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
# 1. Recommended (Per-plot via `ax.legend` or `plt.legend` with `fontsize` & `title_fontsize`):
fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")
ax.plot(x, np.sin(x), label="Sine Wave")
ax.plot(x, np.cos(x), label="Cosine Wave")
ax.legend(
title="Signals",
fontsize=12, # Font size of legend entry labels (points or named size like "large")
title_fontsize=14, # Font size of the legend title (controlled separately from `fontsize`)
loc="upper right",
)
plt.show()
# 2. Global default across all plots in a script/notebook:
plt.rcParams["legend.fontsize"] = 12
plt.rcParams["legend.title_fontsize"] = 14
Key Details & Edge Cases
- Legend title size is NOT scaled by
fontsize=automatically:- Setting
ax.legend(title="Signals", fontsize=16)enlarges the item labels ("Sine Wave","Cosine Wave"), while leaving the"Signals"header at the default size (10pt) unless you also passtitle_fontsize=18(or setplt.rcParams["legend.title_fontsize"]).
- Setting
- Numeric point sizes vs. relative string keywords:
fontsizeaccepts either an exact number in points (12,14.5) or Matplotlib’s relative font keywords:"xx-small","x-small","small","medium"(default10),"large","x-large", and"xx-large".
- Legacy
prop={"size": 14}dictionary syntax vs.fontsize=14:- Older codebases pass
plt.legend(prop={"size": 14}). Avoid combiningfontsize=andprop={"size": ...}in the samelegend()call—ifpropspecifies"size",proptakes precedence overfontsize=; useprop={"size": 14, "weight": "bold"}only when you also need font weight or family customization.
- Older codebases pass
- Adjusting legend marker/handle size alongside text (
markerscale=):- Increasing
fontsize=16enlarges the text labels without resizing scatter dots or line markers inside the legend box. Pairfontsize=16withmarkerscale=1.5so legend symbols stay visually proportional to the larger text.
- Increasing
- Seaborn figure-level plots (
sns.relplot,sns.catplot,sns.lmplot):- Figure-level Seaborn plots attach their legend to the
FacetGrid(g._legend) rather thanax.legend(). Resize Seaborn’s figure-level legend viasns.move_legend(g, "upper right", fontsize=13, title_fontsize=15)org._legend.set_title(..., prop={"size": 15})andplt.setp(g._legend.get_texts(), fontsize=13).
- Figure-level Seaborn plots attach their legend to the
Comparison of Methods to Control Legend Font Size in Matplotlib
| Method | Scope | Controls Legend Entry Text? | Controls Legend Title Text? | Best Used When |
|---|---|---|---|---|
ax.legend(fontsize=14, title_fontsize=16) (Recommended) |
Single Axes / subplot |
Yes (fontsize) |
Yes (title_fontsize) |
Customizing a specific subplot’s legend cleanly using Matplotlib’s object-oriented API. |
plt.legend(fontsize=14, title_fontsize=16) |
Currently active Axes |
Yes (fontsize) |
Yes (title_fontsize) |
Quick single-plot scripts using the pyplot state-machine interface. |
plt.rcParams["legend.fontsize"] = 14 |
All subsequent figures in session | Yes | No (set "legend.title_fontsize" too) |
Enforcing consistent legend typography across a multi-figure report, paper, or notebook. |
ax.legend(prop={"size": 14, "weight": "bold"}) |
Single Axes / subplot |
Yes | No (use title_fontsize) |
Simultaneously setting font size, bold weight, or custom font family on legend labels. |
You must log in or register to comment.
