How to Change Matplotlib Legend Font Size

If you’ve been working with Python’s Matplotlib for a while, you know how important the legend is in making your plots understandable. But sometimes, the default legend font size just doesn’t cut it, maybe it’s too small to read on a presentation slide or too large and distracting in a report.

In this guide, I’ll walk you through multiple ways to change the legend font size in Matplotlib, sharing practical tips from my experience so you can apply them right away.

Let’s get started!

Methods to Adjust Legend Font Size?

Before jumping into the how, let’s quickly touch on the why. Legends describe what each color, marker, or line means in your plot. If your audience can’t easily read the legend, the plot loses much of its value.

For example, when presenting sales data for different US states, a tiny legend can make it difficult for your audience to distinguish between states or product categories. On the other hand, an overly large legend can clutter your chart and distract from the data itself.

Adjusting the font size gives you control over the balance between readability and aesthetics.

Read Add Text to Plot Matplotlib in Python

Method 1: Use the fontsize Parameter in legend()

The simplest way to change the legend font size is by passing the fontsize argument directly to the legend() method in Python.

Here’s a quick example using sales data for three US regions:

import matplotlib.pyplot as plt

regions = ['Northeast', 'Midwest', 'South']
sales = [250, 300, 400]

plt.bar(regions, sales, color=['blue', 'green', 'red'])
plt.legend(['Sales by Region'], fontsize=14)  # Set legend font size to 14
plt.title('Quarterly Sales by Region')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib legend size

In this example, fontsize=14 makes the legend text larger and easier to read, especially when presenting on a projector or sharing screenshots.

You can set the font size to any integer or string value like 'small', 'medium', 'large'. I usually prefer numeric values for precise control.

Check out Matplotlib Bar Chart Labels

Method 2: Use the prop Parameter with Font Properties

If you want more flexibility, such as changing the font family or weight along with the size, use the prop parameter with a FontProperties object in Python.

Example:

from matplotlib import font_manager

font_prop = font_manager.FontProperties(size=16, weight='bold', family='Arial')

plt.bar(regions, sales, color=['blue', 'green', 'red'])
plt.legend(['Sales by Region'], prop=font_prop)
plt.title('Quarterly Sales by Region')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib legend font size

This method is useful when you want your legend to match specific corporate branding or presentation style guides, which often require specific fonts and weights.

Read Matplotlib Save as PNG

Method 3: Change Legend Font Size After Creation

Sometimes, you create your legend first and want to modify the font size afterward. You can do this by accessing the legend object and setting font sizes for each text element.

Here’s how:

fig, ax = plt.subplots()
ax.bar(regions, sales, color=['blue', 'green', 'red'])
legend = ax.legend(['Sales by Region'])

for text in legend.get_texts():
    text.set_fontsize(18)

plt.title('Quarterly Sales by Region')
plt.show()

I executed the above example code and added the screenshot below.

plt legend size

This approach is handy if you generate legends dynamically and want to tweak the font size conditionally.

Check out Matplotlib savefig Blank Image

Method 4: Global Font Size Change Using rcParams

If you want to set the legend font size globally for all plots in your script or session, modify the rcParams dictionary before plotting.

import matplotlib as mpl

mpl.rcParams['legend.fontsize'] = 12

plt.bar(regions, sales, color=['blue', 'green', 'red'])
plt.legend(['Sales by Region'])
plt.title('Quarterly Sales by Region')
plt.show()

This is particularly useful when creating multiple plots with consistent styling, such as in reports or dashboards.

Read Matplotlib Title Font Size

Pro Tips from My Experience

  • Use font sizes between 10 and 16 for most presentations and reports. Smaller than 10 can be hard to read, and larger than 16 can overwhelm the plot.
  • When plotting many legend entries (e.g., multiple product lines), consider using ncol in legend() to arrange entries in multiple columns and keep the legend compact.
  • If your legend overlaps the plot, adjust its location with the loc parameter or use bbox_to_anchor for fine positioning.
  • Remember that font size is just one part of legend readability. Color contrast, background, and spacing also matter.

Adjusting the legend font size in Matplotlib is a small step that makes a big difference in how your audience interprets your charts. Whether you’re presenting quarterly sales data for US regions or visualizing customer demographics, clear legends help tell your story better.

Try these methods in your next project, and you’ll see how much more professional your plots look. If you want to dive deeper into Matplotlib customization, keep exploring other legend properties and styling options.

You may also like to read the other Matplotlib articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.