0
0
MatplotlibHow-ToBeginner ยท 3 min read

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() or ax.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 loc without bbox_to_anchor when precise placement is needed.
  • Setting bbox_to_anchor coordinates without adjusting loc, 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

ParameterDescriptionExample Values
locPosition of legend inside or outside plot'upper right', 'lower left', 'center', 0-10 (numeric codes)
bbox_to_anchorCustom box coordinates to place legend(1, 1), (0.5, 0.5), (1.05, 1)
plt.legend()Function to add legend to current plotplt.legend(loc='best')
ax.legend()Method to add legend to specific axesax.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.