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
Drawing multiple legends on the same axes in Matplotlib
To draw multiple legends on the same axes in Matplotlib, we can create separate legends for different groups of lines and position them at different locations on the plot.
Steps to Create Multiple Legends
- Set the figure size and adjust the padding between and around the subplots
- Plot lines using different labels, linewidth and linestyle
- Create the first legend and place it at the upper-right location
- Add the first legend as an artist to the current axis using
add_artist() - Create the second legend and place it at the lower-right location
- Display the figure using
show()method
Example
Here's how to create multiple legends on the same plot ?
import matplotlib.pyplot as plt # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Plot two lines with different styles line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--') line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4) # Create first legend for line1 first_legend = plt.legend(handles=[line1], loc='upper right') # Add first legend as an artist to preserve it plt.gca().add_artist(first_legend) # Create second legend for line2 plt.legend(handles=[line2], loc='lower right') plt.show()
How It Works
The key to multiple legends is using add_artist() method. When you call plt.legend() a second time, Matplotlib replaces the first legend unless you explicitly add it as an artist to the axes.
Alternative Approach with Custom Positioning
You can also specify exact coordinates for legend placement ?
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
# Plot multiple lines
line1, = plt.plot([1, 2, 3, 4], [1, 4, 2, 3], 'r-', label='Red Line')
line2, = plt.plot([1, 2, 3, 4], [2, 3, 4, 1], 'b--', label='Blue Line')
line3, = plt.plot([1, 2, 3, 4], [3, 1, 4, 2], 'g:', label='Green Line')
# First legend - top right
legend1 = plt.legend(handles=[line1], loc='upper right')
plt.gca().add_artist(legend1)
# Second legend - bottom left using coordinates
legend2 = plt.legend(handles=[line2, line3], bbox_to_anchor=(0.02, 0.02), loc='lower left')
plt.title('Multiple Legends Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Key Points
-
add_artist()is essential to preserve the first legend when adding a second one - Each legend can contain different line handles and be positioned independently
- Use
bbox_to_anchorfor precise positioning with coordinates - The
locparameter works in conjunction withbbox_to_anchorfor fine-tuned placement
Conclusion
Multiple legends in Matplotlib require using add_artist() to preserve earlier legends. This technique allows you to group different plot elements logically and position legends wherever needed on your plot.
Advertisements
