How to Position Legend in Matplotlib: Simple Guide
In Matplotlib, you position the legend using the
loc parameter inside plt.legend() or ax.legend(). You can specify common positions like 'upper right' or use coordinates with bbox_to_anchor for precise placement.Syntax
The basic syntax to position a legend in Matplotlib is:
loc: Defines the legend location using keywords or numeric codes.bbox_to_anchor: Allows custom positioning by specifying a box coordinate.plt.legend()orax.legend(): Functions to add the legend to the plot.
python
plt.legend(loc='upper right', bbox_to_anchor=(1, 1))
Example
This example shows how to place the legend at different positions using loc and how to use bbox_to_anchor for custom placement.
python
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [15, 18, 22, 27] plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') # Position legend at upper left plt.legend(loc='upper left') plt.title('Legend at upper left') plt.show() # Position legend outside the plot using bbox_to_anchor plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') plt.legend(loc='upper left', bbox_to_anchor=(1, 1)) plt.title('Legend outside upper left') plt.tight_layout() plt.show()
Output
Two plots appear: first with legend inside upper left corner; second with legend outside plot at upper left corner.
Common Pitfalls
Common mistakes when positioning legends include:
- Using
locwithoutbbox_to_anchorwhen precise placement is needed. - Setting
bbox_to_anchorcoordinates without adjustingloc, causing unexpected legend placement. - Not calling
plt.tight_layout()when legend is outside plot, which can cause clipping.
python
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] plt.plot(x, y, label='Data') # Wrong: bbox_to_anchor without loc defaults to 'upper center', may confuse placement plt.legend(bbox_to_anchor=(1, 0.5)) plt.title('Incorrect legend placement') plt.show() # Right: specify loc with bbox_to_anchor for clear placement plt.plot(x, y, label='Data') plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.title('Correct legend placement') plt.tight_layout() plt.show()
Output
Two plots appear: first with legend misplaced; second with legend correctly placed outside center left.
Quick Reference
| Parameter | Description | Example Values |
|---|---|---|
| loc | Position of legend inside or outside plot | 'upper right', 'lower left', 'center', 0-10 (numeric codes) |
| bbox_to_anchor | Custom box coordinates to place legend | (1, 1), (0.5, 0.5), (1.05, 1) |
| plt.legend() | Function to add legend to current plot | plt.legend(loc='best') |
| ax.legend() | Method to add legend to specific axes | ax.legend(loc='upper left') |
Key Takeaways
Use the loc parameter in plt.legend() to quickly position the legend at common locations.
Use bbox_to_anchor with loc for precise control of legend placement outside or inside the plot.
Always call plt.tight_layout() when placing legends outside the plot to avoid clipping.
Common loc values include 'upper right', 'lower left', 'center', and numeric codes 0-10.
Specify both loc and bbox_to_anchor together to avoid unexpected legend positions.