Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to position and align a Matplotlib figure legend?
To position and align a Matplotlib figure legend, you can control both its location and alignment using various parameters. The bbox_to_anchor parameter provides precise positioning control, while ncol and loc handle alignment and layout.
Basic Legend Positioning
First, let's create a simple plot with two lines and position the legend ?
import matplotlib.pyplot as plt
# Create sample data
x = [1, 2, 3, 4]
y1 = [1, 5, 1, 7]
y2 = [5, 1, 7, 1]
# Plot two lines
line1, = plt.plot(x, y1, linewidth=2, label='Line 1')
line2, = plt.plot(x, y2, linewidth=2, label='Line 2')
# Position legend at top with horizontal layout
plt.legend(bbox_to_anchor=(0.5, 1.0), loc='lower center', ncol=2)
plt.title('Legend Positioned Above Plot')
plt.show()
Different Legend Positions
You can position the legend in various locations using bbox_to_anchor coordinates ?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
x = [1, 2, 3, 4]
y1 = [1, 5, 1, 7]
y2 = [5, 1, 7, 1]
# Top-left: Legend above plot
axes[0,0].plot(x, y1, label='Line 1')
axes[0,0].plot(x, y2, label='Line 2')
axes[0,0].legend(bbox_to_anchor=(0.5, 1.1), loc='center', ncol=2)
axes[0,0].set_title('Above Plot')
# Top-right: Legend to the right
axes[0,1].plot(x, y1, label='Line 1')
axes[0,1].plot(x, y2, label='Line 2')
axes[0,1].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
axes[0,1].set_title('Right Side')
# Bottom-left: Legend below plot
axes[1,0].plot(x, y1, label='Line 1')
axes[1,0].plot(x, y2, label='Line 2')
axes[1,0].legend(bbox_to_anchor=(0.5, -0.1), loc='upper center', ncol=2)
axes[1,0].set_title('Below Plot')
# Bottom-right: Legend inside plot
axes[1,1].plot(x, y1, label='Line 1')
axes[1,1].plot(x, y2, label='Line 2')
axes[1,1].legend(loc='upper right')
axes[1,1].set_title('Inside Plot')
plt.tight_layout()
plt.show()
Legend Alignment Options
| Parameter | Purpose | Common Values |
|---|---|---|
bbox_to_anchor |
Position coordinates | (0.5, 1.0) for top center |
loc |
Anchor point | 'upper left', 'center', 'lower right' |
ncol |
Number of columns | 1 (vertical), 2+ (horizontal) |
Custom Legend Styling
You can also customize the legend's appearance and spacing ?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 5, 1, 7]
y2 = [5, 1, 7, 1]
y3 = [3, 3, 5, 3]
plt.figure(figsize=(8, 6))
plt.plot(x, y1, linewidth=2, label='Dataset A')
plt.plot(x, y2, linewidth=2, label='Dataset B')
plt.plot(x, y3, linewidth=2, label='Dataset C')
# Custom legend with styling
plt.legend(
bbox_to_anchor=(0.5, -0.1),
loc='upper center',
ncol=3,
frameon=True,
shadow=True,
title='Legend Title',
title_fontsize=12
)
plt.title('Styled Legend Below Plot')
plt.tight_layout()
plt.show()
Conclusion
Use bbox_to_anchor for precise legend positioning and ncol for horizontal layouts. The loc parameter determines which part of the legend anchors to your specified coordinates, giving you full control over legend placement and alignment.
Advertisements
