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 modify a Matplotlib legend after it has been created?
Matplotlib legends can be modified after creation using various methods. You can change the title, position, styling, and other properties of an existing legend object.
Basic Legend Modification
First, let's create a basic plot with a legend and then modify it ?
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 5))
# Create a plot with labels
plt.plot([1, 3, 4, 5, 2, 1], [3, 4, 1, 3, 0, 1],
label="line plot", color='red', linewidth=2)
plt.plot([2, 4, 3, 6, 1, 2], [1, 2, 4, 2, 3, 1],
label="second line", color='blue', linewidth=2)
# Create legend and store reference
legend = plt.legend(loc="upper right")
# Modify the legend after creation
legend.set_title("Plot Legend", prop={'size': 12, 'weight': 'bold'})
plt.title("Modifying Legend After Creation")
plt.show()
Common Legend Modifications
Changing Legend Properties
You can modify various aspects of the legend using different methods ?
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
# Create multiple plots
x = [1, 2, 3, 4, 5]
plt.plot(x, [1, 4, 2, 5, 3], 'ro-', label='Dataset A', linewidth=2)
plt.plot(x, [2, 1, 4, 3, 5], 'bs-', label='Dataset B', linewidth=2)
plt.plot(x, [3, 3, 3, 3, 3], 'g^-', label='Average', linewidth=2)
# Create legend
legend = plt.legend(loc="upper left")
# Modify legend properties
legend.set_title("Data Comparison", prop={'size': 14, 'weight': 'bold'})
legend.get_frame().set_facecolor('lightgray')
legend.get_frame().set_alpha(0.8)
legend.get_frame().set_edgecolor('black')
plt.title("Modified Legend with Custom Styling")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.grid(True, alpha=0.3)
plt.show()
Advanced Legend Modifications
Changing Legend Text and Position
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
# Create plots
plt.plot([1, 2, 3, 4], [1, 4, 2, 3], 'o-', label='Original Label')
plt.plot([1, 2, 3, 4], [2, 1, 3, 4], 's-', label='Another Label')
# Create legend
legend = plt.legend()
# Modify legend text
legend_texts = legend.get_texts()
legend_texts[0].set_text('Modified First Label')
legend_texts[1].set_text('Modified Second Label')
# Change legend position and styling
legend.set_bbox_to_anchor((1.05, 1))
legend.set_title("Modified Legend")
legend.get_frame().set_linewidth(2)
plt.title("Dynamically Modified Legend Labels")
plt.tight_layout()
plt.show()
Methods for Legend Modification
| Method | Purpose | Example Usage |
|---|---|---|
set_title() |
Set legend title | legend.set_title("Title") |
get_frame() |
Access legend frame | legend.get_frame().set_alpha(0.5) |
get_texts() |
Get legend text objects | legend.get_texts()[0].set_text("New") |
set_bbox_to_anchor() |
Position legend | legend.set_bbox_to_anchor((1, 1)) |
Conclusion
Store the legend object when calling plt.legend() to modify it later. Use methods like set_title(), get_frame(), and get_texts() to customize appearance and content dynamically.
Advertisements
