How to Place Legend Outside of the Plot in Matplotlib?
Placing the legend outside of the plot in Matplotlib helps make the chart cleaner and easier to read, especially when dealing with multiple lines or subplots. Instead of cluttering the plot area, the legend can be positioned beside or above the plot, giving more space to the data. Let’s explore different methods to do this efficiently .
Using fig.legend()
fig.legend() method is helpful when you have multiple plots in one figure and want to show one common legend for all of them. It places the legend at the figure level instead of just one plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axs = plt.subplots(1, 2)
axs[0].plot(x, np.sin(x), label='sin(x)')
axs[1].plot(x, np.cos(x), label='cos(x)')
fig.legend(bbox_to_anchor=(1.1, 1), loc='upper right')
fig.tight_layout()
plt.show()
Output

Explanation: fig.legend() creates a shared legend for all subplots. bbox_to_anchor=(1.1, 1) places the legend outside the top-right corner, and loc='upper right' aligns it. fig.tight_layout() ensures proper spacing so that the plots don’t overlap with the legend.
Using ax.legend()
ax.legend() method is used when you want to place the legend near a specific plot, especially if you're working with subplots. It gives you more control over the legend’s position, even outside the plot area.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label='sin(x)')
ax.plot(x, np.cos(x), label='cos(x)')
ax.legend(loc='center', bbox_to_anchor=(0.9, 0.5), bbox_transform=fig.transFigure)
plt.tight_layout()
plt.show()
Output

Explanation: ax.legend() adds the legend to a specific subplot. bbox_to_anchor=(0.9, 0.5) places it outside the plot, aligned to the center by loc='center'. bbox_transform=fig.transFigure makes the position relative to the entire figure and plt.tight_layout() adjusts spacing to avoid overlap.
Using plt.legend()
plt.legend() method is the simplest and is used when you're making a basic plot. It’s easy to use and works well for quick, single-plot charts, but it doesn't offer much control for more complex layouts.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper right')
plt.tight_layout()
plt.show()
Output

Explanation: plt.legend() adds the legend to the entire plot. bbox_to_anchor=(1.05, 1) positions it just outside the top-right corner and loc='upper right' aligns it to that point. plt.tight_layout() ensures the plot elements are spaced properly and don't overlap with the legend.
Using fig.add_artist()
fig.add_artist() method is used in special cases where the legend disappears or gets cut off, often when using tight_layout(). You create the legend and manually add it back to the figure to make sure it shows correctly.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots()
sine, = ax.plot(x, np.sin(x), label='sin(x)')
cosine, = ax.plot(x, np.cos(x), label='cos(x)')
leg = plt.legend(handles=[sine, cosine], loc='center left', bbox_to_anchor=(1.0, 0.5))
fig.add_artist(leg)
plt.tight_layout()
plt.show()
Output

Explanation: Legend is manually created using plt.legend() with specified plot handles. bbox_to_anchor=(1.0, 0.5) places it outside the plot on the left center and loc='center left' aligns it accordingly. Since tight_layout() can sometimes hide or clip the legend, fig.add_artist(leg) is used to manually add the legend back to the figure.