2025, Nov 11 17:00
Enlarge Matplotlib Legend Text and Scatter Marker Size: markerscale and PathCollection set_sizes
Learn how to enlarge Matplotlib legend text and scatter markers using markerscale or PathCollection set_sizes for export-ready figures. Step-by-step code.
When preparing Matplotlib figures for export, increasing legend font size is straightforward, but the scatter symbol in the legend often stays tiny. The usual text-only adjustments leave the marker unchanged, which makes a polished figure look inconsistent. Here is how to scale both the legend text and the scatter marker correctly.
Reproducing the issue
The intent is to enlarge the legend text, but the dot next to the label remains small:
import matplotlib.pyplot as plt
def grow_legend(ax_obj, text_size=25):
for t in ax_obj.get_legend().get_texts():
t.set_fontsize(text_size)
fig, axe = plt.subplots()
axe.scatter([1, 2, 3], [2, 3, 1], label="auto")
axe.legend()
grow_legend(axe)
This does increase the font size, yet the marker size in the legend does not change.
What is really happening
The symbol placed in the legend for a scatter plot is a PathCollection, not a Line2D or Patch. That is why get_patches and get_lines return empty sequences for this legend. The marker you see is not a line or a patch; it is the scatter collection’s handle. To scale that symbol, you either set markerscale when creating the legend or change the collection’s sizes after the legend is created.
Two practical ways to fix it
The most direct approach is to control the symbol at legend creation with markerscale. This scales the legend marker relative to the plotted marker size and pairs naturally with fontsize:
import matplotlib.pyplot as plt
fig, axis = plt.subplots()
axis.scatter([1, 2, 3], [2, 3, 1], label='auto')
# Scale both text and the legend dot
axis.legend(fontsize=25, markerscale=3)
If you prefer a helper that adjusts an existing legend, you can modify both the text and the scatter handle sizes. The key is to access the legend handles and call set_sizes with an area specified in points²:
import matplotlib.pyplot as plt
def resize_legend(axi, txt_size=25, dot_area=200):
leg = axi.get_legend()
for item in leg.get_texts():
item.set_fontsize(txt_size)
# Handle attribute name differences across Matplotlib versions
handles = getattr(leg, "legendHandles", getattr(leg, "legend_handles", []))
for h in handles:
h.set_sizes([dot_area])
fig, axis = plt.subplots()
axis.scatter([1, 2, 3], [2, 3, 1], label='auto')
axis.legend()
resize_legend(axis)
Both approaches enlarge the legend marker to match the visual weight of the text, producing an export-ready legend.
Why this matters
Legends are part of the figure’s visual hierarchy. When text size increases but symbols remain small, the legend becomes unbalanced and harder to scan. Scaling markers along with labels keeps the figure consistent and improves readability in reports and publications.
Recommendations
Choose markerscale when you already know the target size while creating the legend. If you need to retrofit existing plots or want explicit control over the marker area in points², adjust the legend handles and call set_sizes on the PathCollection. In environments with varying Matplotlib versions, access legend handles defensively, as shown above, to avoid attribute errors.
Aligning text and markers in the legend is a small change with a big payoff for clarity and presentation quality.