Matplotlib Legend Font Size

Matplotlib Legend Font Size

Matplotlib is a popular plotting library in Python that allows users to create a wide range of visualizations. Legends are an important part of any plot as they help to explain the different elements present in the plot. One common customization that users often want to make is adjusting the font size of the legend in their plots. In this article, we will explore how to change the font size of the legend in Matplotlib.

Setting the Font Size for the Legend

You can adjust the font size of the legend in Matplotlib by using the fontsize parameter within the plt.legend() function. Here is an example of how to set the font size of the legend to 12:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4], label='Line 1')
plt.legend(fontsize=12)
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of the legend is set to 12. You can adjust the font size to your desired value by changing the value of the fontsize parameter.

Changing the Font Size of the Legend Text

You can also change the font size of the text within the legend in Matplotlib by using the prop parameter within the plt.legend() function. Here is an example of how to set the font size of the legend text to 14:

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

font = FontProperties(size=14)

plt.plot([1, 2, 3, 4], label='Line 1')
plt.legend(prop=font)
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of the text within the legend is set to 14. You can adjust the font size to your desired value by changing the value of the size parameter in the FontProperties object.

Customizing the Font Size for Each Legend Item

If you want to customize the font size for each item in the legend separately, you can do so by creating a dictionary of font properties and passing it to the prop parameter. Here is an example of how to set different font sizes for each legend item:

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

font_dict = {
    'size': 12,
    'weight': 'bold',
    'family': 'serif'
}

font = FontProperties(**font_dict)

plt.plot([1, 2, 3, 4], label='Line 1')
plt.plot([4, 3, 2, 1], label='Line 2')
plt.legend(prop={'Line 1': font, 'Line 2': {'size': 14}})
plt.show()

In this example, the font size of ‘Line 1’ in the legend is set to 12 with a bold serif font, while the font size of ‘Line 2’ is set to 14. You can customize the font properties for each legend item by providing a dictionary of font properties within the prop parameter.

Changing the Font Size using rcParams

Another way to change the font size of the legend in Matplotlib is by using the rcParams configuration. You can set the default font size for the legend using the legend.fontsize parameter. Here is an example of how to set the default font size for the legend to 16 using rcParams:

import matplotlib.pyplot as plt
import matplotlib as mpl

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

plt.plot([1, 2, 3, 4], label='Line 1')
plt.legend()
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the default font size for the legend is set to 16 using rcParams. This will apply to all legends in the plot unless overridden by local settings.

Adjusting the Font Size in a Seaborn Plot with Matplotlib

If you are using Seaborn, a statistical data visualization library built on top of Matplotlib, you can still adjust the font size of the legend by accessing the matplotlib parameters. Here is an example of how to set the font size of the legend in a Seaborn plot:

import seaborn as sns
import matplotlib.pyplot as plt

sns.lineplot(x=[1, 2, 3, 4], y=[5, 4, 3, 2])
plt.legend(fontsize=14)
plt.show()

In this example, the font size of the legend in the Seaborn plot is set to 14 using the fontsize parameter within the plt.legend() function.

Changing the Font Size of the Legend Title

You can also adjust the font size of the legend title in Matplotlib by using the title_fontsize parameter within the plt.legend() function. Here is an example of how to set the font size of the legend title to 16:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4], label='Line 1')
plt.legend(title='Legend', title_fontsize=16)
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of the legend title is set to 16 using the title_fontsize parameter within the plt.legend() function.

Fine-tuning the Font Size of the Legend Text

If you want more precise control over the font size of the legend text, you can adjust the font size of each text element individually using the set_size method. Here is an example of how to set the font size of the legend text for each item in the legend:

import matplotlib.pyplot as plt

legend = plt.legend(['Line 1', 'Line 2'])
for text in legend.get_texts():
    text.set_fontsize(12)
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of each text element in the legend is set to 12 using the set_fontsize method. You can customize the font size for each text element in the legend by iterating through the text elements using legend.get_texts().

Changing the Font Size of the Legend Box

You can also change the font size of the legend box in Matplotlib by adjusting the font size of the legend border using the set_frame_on method. Here is an example of how to set the font size of the legend box to 10:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4], label='Line 1')
plt.legend(fontsize=12)
legend = plt.gca().get_legend()
legend.set_frame_on(True)
legend.get_frame().set_facecolor('white')
legend.get_frame().set_edgecolor('black')
legend.get_frame().set_linewidth(2)
plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of the legend box is set to 12, and the border of the legend box is customized using the set_frame_on, set_facecolor, set_edgecolor, and set_linewidth methods.

Adjusting the Font Size of the Legend in Subplots

If you are working with subplots in Matplotlib and want to adjust the font size of the legends for each subplot, you can do so by accessing the legend object for each subplot. Here is an example of how to set the font size of the legend in subplots:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

for ax in axs.flat:
    ax.plot([1, 2, 3, 4], label='Line')
    legend = ax.legend()
    for text in legend.get_texts():
        text.set_fontsize(10)

plt.show()

Output:

Matplotlib Legend Font Size

In this example, the font size of the legend in each subplot is set to 10 by iterating through the text elements in the legend object for each subplot.

Conclusion

Adjusting the font size of the legend in Matplotlib is a common customization that users often want to make in their plots. By using the various methods and parameters available in Matplotlib, you can easily change the font size of the legend text, title, box, and individual items. Whether you are working with standalone plots or subplots, you can fine-tune the font size of the legend to suit your visualization needs.

By following the examples provided in this article, you should now have a good understanding of how to adjust the font size of the legend in Matplotlib and create visually appealing plots with customized legends.Experiment with different font sizes to find the perfect balance for your plots.

Like(0)