Controlling Legend Font Size in Matplotlib

Matplotlib is a powerful Python library for creating static, interactive, and animated visualizations. A crucial aspect of creating effective plots is customizing the appearance of plot elements, including the legend. This tutorial focuses on how to control the font size of the legend in your Matplotlib plots.

Understanding the Legend

The legend in a plot explains the meaning of different markers, colors, or line styles used to represent data. A clear and readable legend is vital for understanding the visualization. Controlling the font size ensures that the legend text is neither too large (cluttering the plot) nor too small (making it difficult to read).

Methods for Adjusting Font Size

There are several ways to adjust the font size of your Matplotlib legend. We’ll explore the most common and flexible approaches.

1. Using the fontsize Argument in plt.legend()

The simplest way to control the legend font size is by directly specifying the fontsize argument within the plt.legend() function. You can provide either a numerical value (in points) or a predefined size string.

import matplotlib.pyplot as plt

# Sample data
k = [1, 2, 3, 4, 5]
sum_cf = [0.5, 0.6, 0.7, 0.8, 0.9]
data = [[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1.0], [0.2, 0.3, 0.4, 0.5, 0.6]]

# Create the plot
plt.figure()
plt.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plt.scatter(k, data[0], color='b', label='Dis 1: cf = .6, var = .2')
plt.scatter(k, data[1], color='r', label='Dis 2: cf = .2, var = .1')
plt.scatter(k, data[2], color='g', label='Dis 3: cf = .1, var = .01')

# Create the legend with a specific font size
plt.legend(loc=2, fontsize=10)  # Using a numerical size in points
# or
# plt.legend(loc=2, fontsize='small')  # Using a predefined size string

plt.show()

Predefined size strings include: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', and 'xx-large'.

2. Using rcParams for Global Settings

For more consistent styling across multiple plots, you can modify the rcParams (runtime configuration parameters) of Matplotlib. This allows you to set a default font size for all legends in your script.

import matplotlib.pyplot as plt

# Set the default legend font size
plt.rc('legend', fontsize=12)

# Sample data (same as before)
k = [1, 2, 3, 4, 5]
sum_cf = [0.5, 0.6, 0.7, 0.8, 0.9]
data = [[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1.0], [0.2, 0.3, 0.4, 0.5, 0.6]]

# Create the plot
plt.figure()
plt.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plt.scatter(k, data[0], color='b', label='Dis 1: cf = .6, var = .2')
plt.scatter(k, data[1], color='r', label='Dis 2: cf = .2, var = .1')
plt.scatter(k, data[2], color='g', label='Dis 3: cf = .1, var = .01')

# Create the legend (font size will be the default set in rcParams)
plt.legend(loc=2)

plt.show()

This method ensures that all subsequent legends in your script will use the specified font size unless explicitly overridden in the plt.legend() call.

3. Using a Dictionary with prop Argument

You can also utilize the prop argument within plt.legend() to pass a dictionary containing font properties.

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# Create a FontProperties object
font_properties = FontProperties(size=14)

# Sample data
k = [1, 2, 3, 4, 5]
sum_cf = [0.5, 0.6, 0.7, 0.8, 0.9]
data = [[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1.0], [0.2, 0.3, 0.4, 0.5, 0.6]]

# Create the plot
plt.figure()
plt.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plt.scatter(k, data[0], color='b', label='Dis 1: cf = .6, var = .2')
plt.scatter(k, data[1], color='r', label='Dis 2: cf = .2, var = .1')
plt.scatter(k, data[2], color='g', label='Dis 3: cf = .1, var = .01')

# Create the legend with custom font properties
plt.legend(loc=2, prop=font_properties)

plt.show()

This approach provides more granular control over the legend’s appearance, allowing you to customize other font attributes like weight, style, and family.

4. Adjusting Legend Title Font Size (Matplotlib 3.4.2+)

In Matplotlib version 3.4.2 and later, you can independently control the font size of the legend title using the title_fontsize argument in plt.legend().

import matplotlib.pyplot as plt

# Sample data (same as before)
k = [1, 2, 3, 4, 5]
sum_cf = [0.5, 0.6, 0.7, 0.8, 0.9]
data = [[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1.0], [0.2, 0.3, 0.4, 0.5, 0.6]]

# Create the plot
plt.figure()
plt.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plt.scatter(k, data[0], color='b', label='Dis 1: cf = .6, var = .2')
plt.scatter(k, data[1], color='r', label='Dis 2: cf = .2, var = .1')
plt.scatter(k, data[2], color='g', label='Dis 3: cf = .1, var = .01')

# Create the legend with custom font sizes for items and title
plt.legend(loc=2, fontsize=10, title_fontsize=15, title="My Legend")

plt.show()

This allows for even more refined control over the legend’s visual presentation.

By using these methods, you can create clear and readable legends that enhance the effectiveness of your Matplotlib visualizations.

Leave a Reply

Your email address will not be published. Required fields are marked *