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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.