Matplotlib Legend Outside: A Guide to Customizing Legends

Matplotlib Legend Outside: A Guide to Customizing Legends

Matplotlib is a powerful data visualization library in Python that provides a wide range of options to create informative and visually appealing charts and plots. Legends play a crucial role in conveying the meaning of different elements in a plot. By default, the legend is placed inside the plot area, but sometimes you may want to move it outside the plot area for better visibility and clarity.

In this article, we will explore different ways to customize legends in Matplotlib and move them outside the plot area. We will cover various techniques and code examples to achieve this. Let’s get started!

Matplotlib Legend Outside Code Examples

Here are ten code examples demonstrating different techniques to move the legend outside the plot in Matplotlib. Each example is accompanied by the code execution results:

Example 1: Basic Legend Placement

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y, label='sin(x)')
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1))
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

In this example, we use the loc parameter in the plt.legend() function to specify the position of the legend. The bbox_to_anchor parameter allows us to move the legend outside the plot area.

Example 2: Custom Legend Positioning

plt.plot(x, y, label='sin(x)')
plt.legend(loc='upper left', bbox_to_anchor=(0, 1))
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By adjusting the bbox_to_anchor parameter, we shift the legend to the top left corner of the plot.

Example 3: Horizontal Legend Placement

plt.plot(x, y, label='sin(x)')
plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.2), ncol=2)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By specifying ncol=2, we arrange the legend items horizontally in two columns.

Example 4: Empty Space for External Legend

plt.plot(x, y, label='sin(x)')
plt.legend(loc='upper left', bbox_to_anchor=(0.5, 1.15), ncol=2)
plt.subplots_adjust(top=0.8)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By adjusting the top parameter in plt.subplots_adjust(), we create empty space above the plot to accommodate the legend.

Example 5: Legend Box Outside the Plot

plt.plot(x, y, label='sin(x)')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

Setting frameon=False hides the legend box, giving the appearance of the legend being placed outside the plot.

Example 6: Legend with Title

plt.plot(x, y, label='sin(x)')
leg = plt.legend(title='Legend', loc='center right', bbox_to_anchor=(1.25, 0.5))
plt.setp(leg.get_title(), fontsize='large')
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By using plt.setp() function, we can modify the font size of the legend title.

Example 7: Legend with Shadow

plt.plot(x, y, label='sin(x)')
plt.legend(loc='lower left', bbox_to_anchor=(0, -0.3), shadow=True)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

Setting shadow=True adds a shadow effect to the legend.

Example 8: Custom Legend Colors

plt.plot(x, y, label='sin(x)')
plt.legend(loc='center', bbox_to_anchor=(0.5, -0.35), facecolor='lightgray')
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By using the facecolor parameter, we can change the background color of the legend.

Example 9: Legend with Marker Size

plt.plot(x, y, label='sin(x)')
plt.legend(loc='upper right', bbox_to_anchor=(1, 1))
plt.scatter(x[::10], y[::10], label='Points', color='red', s=50)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

In this example, we add a scatter plot using plt.scatter() and customize the marker size with the s parameter.

Example 10: Multiple Legends

plt.plot(x, y, label='sin(x)')
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1))
plt.scatter(x[::10], y[::10], label='Points', color='red', s=50)
plt.legend(loc='center', bbox_to_anchor=(0.5, -0.3), ncol=2)
plt.show()

Output:

Matplotlib Legend Outside: A Guide to Customizing Legends

By calling plt.legend() multiple times, we can create multiple legends in a single plot.

Matplotlib Legend Outside Conclusion

In this article, we have explored different techniques to move the legend outside the plot area in Matplotlib. By adjusting the loc and bbox_to_anchor parameters, we can control the position and placement of the legend. Additionally, we have covered various customization options such as adding titles, shadows, changing background colors, and creating multiple legends.

Matplotlib provides a flexible and powerful toolset to create captivating visualizations with informative legends. With this newfound knowledge, you can now effectively communicate complex data and enhance the readability of your plots.

Like(1)