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
How to adjust the space between legend markers and labels in Matplotlib?
In Matplotlib, you can adjust the spacing between legend markers and their corresponding labels using the labelspacing parameter in the legend() method. This parameter controls the vertical space between legend entries.
Basic Syntax
plt.legend(labelspacing=value)
Where value is a float representing the space in font-size units. The default value is typically 0.5.
Example with Different Label Spacing
Let's create a plot with multiple lines and adjust the spacing between legend entries ?
import matplotlib.pyplot as plt
# Create sample data
x = [0, 1, 2, 3, 4]
y1 = [0, 1, 4, 9, 16]
y2 = [0, 1, 2, 3, 4]
y3 = [0, 0.5, 1, 1.5, 2]
# Plot multiple lines
plt.plot(x, y1, label='Quadratic')
plt.plot(x, y2, label='Linear')
plt.plot(x, y3, label='Half Linear')
# Adjust legend spacing
space = 1.5
plt.legend(labelspacing=space)
plt.title('Legend with Custom Label Spacing')
plt.show()
Comparison of Different Spacing Values
Here's how different labelspacing values affect the legend appearance ?
import matplotlib.pyplot as plt
# Create subplots to compare different spacing
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
x = [0, 1, 2]
y1 = [1, 2, 3]
y2 = [2, 3, 4]
y3 = [3, 4, 5]
spacing_values = [0.2, 1.0, 2.0]
titles = ['Tight Spacing (0.2)', 'Default Spacing (1.0)', 'Wide Spacing (2.0)']
for i, (ax, spacing, title) in enumerate(zip(axes, spacing_values, titles)):
ax.plot(x, y1, label='Series 1')
ax.plot(x, y2, label='Series 2')
ax.plot(x, y3, label='Series 3')
ax.legend(labelspacing=spacing)
ax.set_title(title)
plt.tight_layout()
plt.show()
Parameters Summary
| Parameter | Type | Description | Default |
|---|---|---|---|
labelspacing |
float | Vertical space between legend entries | 0.5 |
columnspacing |
float | Spacing between columns | 2.0 |
handletextpad |
float | Space between marker and text | 0.8 |
Advanced Legend Spacing Control
You can combine multiple spacing parameters for fine-tuned control ?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
plt.plot(x, [i**2 for i in x], 'r-', label='Squared')
plt.plot(x, [i**1.5 for i in x], 'g--', label='Power 1.5')
plt.plot(x, [i**0.5 for i in x], 'b:', label='Square Root')
# Fine-tune legend spacing
plt.legend(
labelspacing=1.2, # Space between entries
handletextpad=0.5, # Space between marker and text
columnspacing=1.0 # Column spacing if multiple columns
)
plt.title('Custom Legend Spacing Parameters')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
Conclusion
Use the labelspacing parameter in plt.legend() to control vertical spacing between legend entries. Values less than 0.5 create tighter spacing, while values greater than 1.0 create more spread-out legends. Combine with other spacing parameters for complete control over legend appearance.
