Matplotlib Legend Size

Matplotlib Legend Size

In this article, we will explore how to change the size of the legend in Matplotlib plots. Legends are an essential part of plots as they help in identifying different elements or data series on the graph. By adjusting the size of the legend, we can improve the readability and aesthetics of our plots.

Change Legend Size

To change the size of the legend in a Matplotlib plot, we can use the fontsize parameter in the legend() function. The fontsize parameter allows us to specify the font size of the legend text. Let’s see how we can do this with an example:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
plt.legend(fontsize=12)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have set the font size of the legend to 12 points using the fontsize parameter in the legend() function. You can adjust the value of fontsize to make the legend text larger or smaller.

Customizing Legend Size

We can further customize the size of the legend in Matplotlib plots by setting the prop parameter in the legend() function. The prop parameter allows us to specify font properties such as size, weight, style, etc. Let’s see how we can customize the legend size with font properties:

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

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
plt.legend(prop=FontProperties(size=14))
plt.show()

Output:

Matplotlib Legend Size

In this example, we have customized the legend size by setting the font size to 14 points using the prop parameter in the legend() function. We have used the FontProperties class to specify the font properties of the legend text.

Adjusting Legend Size in Subplots

When working with multiple subplots in Matplotlib, we may need to adjust the size of the legend in each subplot separately. We can achieve this by specifying the fontsize or prop parameter for each legend in the subplots. Let’s see how we can adjust the legend size in subplots:

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

fig, axs = plt.subplots(2)

# Plot for the first subplot
x1 = [1, 2, 3]
y1 = [2, 4, 6]
axs[0].plot(x1, y1, label='y1 = 2x')
axs[0].legend(prop=FontProperties(size=12))

# Plot for the second subplot
x2 = [1, 2, 3]
y2 = [3, 6, 9]
axs[1].plot(x2, y2, label='y2 = 3x')
axs[1].legend(fontsize=14)

plt.show()

Output:

Matplotlib Legend Size

In this example, we have created two subplots using the subplots() function. We have adjusted the legend size for each subplot by specifying the fontsize or prop parameter in the legend() function.

Changing Legend Size with Styles

We can also change the size of the legend in Matplotlib plots by using different styles such as ‘large’, ‘medium’, ‘small’, etc. The styles provide predefined font sizes for the legend text. Let’s see how we can change the legend size with styles:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
plt.legend(fontsize='large')
plt.show()

Output:

Matplotlib Legend Size

In this example, we have changed the legend size by specifying the style ‘large’ in the fontsize parameter of the legend() function. You can use different styles such as ‘medium’, ‘small’, ‘x-small’, etc., to adjust the legend size accordingly.

Setting Legend Size in Relative Terms

We can set the size of the legend in Matplotlib plots in relative terms to the default font size. This allows us to adjust the legend size proportionally to the default font size. We can achieve this by specifying a relative size value for the fontsize parameter. Let’s see how we can set the legend size in relative terms:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
plt.legend(fontsize='1.5em')
plt.show()

In this example, we have set the legend size to 1.5 times the default font size by specifying the relative size value ‘1.5em’ in the fontsize parameter of the legend() function. You can adjust the relative size value to make the legend text larger or smaller.

Changing Legend Size with Text Properties

We can also change the size of the legend in Matplotlib plots by using text properties such as font size, weight, style, etc. We can specify text properties for the legend text using the set_size() method of the Text class. Let’s see how we can change the legend size with text properties:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
legend = plt.legend()
for text in legend.get_texts():
    text.set_size(14)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have changed the size of the legend text by setting the font size to 14 points using the set_size() method of the Text class. We have accessed the legend text using the get_texts() method and then applied the font size to each text element.

Adjust Legend Size with Bbox Transform

We can adjust the size of the legend in Matplotlib plots by using the BboxTransformTo() class to transform the legend size. The BboxTransformTo() class allows us to specify the target bounding box for the legend. Let’s see how we can adjust the legend size with BboxTransformTo():

import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
legend = plt.legend()
for text in legend.get_texts():
    text.set_transform(transforms.BboxTransformTo([[0, 0], [1, 1]]))
plt.show()

In this example, we have adjusted the size of the legend text by transforming the legend using the BboxTransformTo() class. We have specified the target bounding box as [[0, 0], [1, 1]] to resize the legend text within the specified bounds.

Changing Legend Size with Mathtext

We can change the size of the legend text in Matplotlib plots by using Mathtext to render mathematical expressions. Mathtext allows us to specify font size, weight, style, etc., for mathematical text in legends. Let’s see how we can change the legend size with Mathtext:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label=r'y = x^2', linewidth=2)
plt.legend(fontsize=14)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have changed the size of the legend text by rendering the mathematical expression ‘y = x^2’ using Mathtext. We have specified the font size as 14 points using the fontsize parameter in the legend() function.

Adjusting Legend Size with Axes Properties

We can also adjust the size of the legend in Matplotlib plots by using axes properties to modify the legend text size. We can access axes propertiesand apply changes to the legend size. Let’s see how we can adjust the legend size with axes properties:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
plt.legend(fontsize=12)
plt.gca().legend_.set_title(None)
plt.gca().legend_.set_bbox_to_anchor((1, 1))
plt.show()

Output:

Matplotlib Legend Size

In this example, we have adjusted the legend size by using axes properties to modify the legend text. We have set the font size to 12 points using the fontsize parameter in the legend() function. Additionally, we have removed the legend title and repositioned the legend box using axes properties.

Customize Legend Size with Marker Size

We can customize the size of the legend in Matplotlib plots by specifying the marker size for the legend markers. By changing the marker size, we can adjust the overall size of the legend in the plot. Let’s see how we can customize the legend size with marker size:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2', marker='o', markersize=10)
plt.legend(fontsize=12)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have customized the legend size by specifying a marker size of 10 for the legend markers in the plot. By changing the marker size, we have adjusted the overall size of the legend in the plot.

Adjust Legend Size with Title Properties

We can adjust the size of the legend in Matplotlib plots by using title properties to modify the legend title size. We can access title properties and apply changes to the legend title size. Let’s see how we can adjust the legend size with title properties:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
legend = plt.legend(fontsize=12)
legend.set_title('Legend Title', {'size': 16})
plt.show()

Output:

Matplotlib Legend Size

In this example, we have adjusted the size of the legend title by setting the title size to 16 points using title properties. We have accessed the legend title using the set_title() method and specified the font size for the legend title.

Changing Legend Size with Axis Properties

We can change the size of the legend in Matplotlib plots by using axis properties to modify the legend text size. By accessing axis properties, we can adjust the legend size in the plot. Let’s see how we can change the legend size with axis properties:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
legend = plt.legend(fontsize=12)
for item in legend.get_children():
    if isinstance(item, plt.Text):
        item.set_fontsize(14)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have changed the size of the legend text by accessing axis properties and modifying the font size. We have iterated over the children of the legend and adjusted the font size of the legend text to 14 points.

Resize Legend Size with Font Properties

We can resize the legend in Matplotlib plots by using font properties to modify the legend text size. Font properties allow us to specify font size, weight, style, etc., for the legend text. Let’s see how we can resize the legend size with font properties:

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

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y, label='y = x^2')
legend = plt.legend(fontsize=12)
prop = FontProperties(size=16)
for text in legend.get_texts():
    text.set_font_properties(prop)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have resized the legend text by specifying font properties with a font size of 16 points. We have used the FontProperties class to set the font size for the legend text in the plot.

Adjust Legend Size in 3D Plots

When working with 3D plots in Matplotlib, we may need to adjust the size of the legend in the plot. We can resize the legend in 3D plots by specifying the font size for the legend text. Let’s see how we can adjust the legend size in 3D plots:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]

ax.plot(x, y, z, label='y = x^2')
ax.legend(fontsize=12)
plt.show()

Output:

Matplotlib Legend Size

In this example, we have adjusted the size of the legend in a 3D plot by specifying the font size for the legend text. We have used the fontsize parameter in the legend() function to set the font size to 12 points in the 3D plot.

Conclusion

In this article, we have explored different methods to change the size of the legend in Matplotlib plots. We have learned how to customize the legend size using fontsize, prop, styles, relative terms, text properties, Mathtext, axes properties, marker size, title properties, axis properties, and font properties. By adjusting the legend size, we can enhance the readability and aesthetics of our plots. Experiment with these techniques in your plots to create visually appealing and informative visualizations.

Like(0)