0
0
Matplotlibdata~10 mins

Multiple legends in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple legends
Create first plot elements
Create first legend
Create second plot elements
Create second legend
Display plot with both legends
First, plot data and create the first legend. Then plot more data and create a second legend. Finally, show the plot with both legends visible.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

line1, = plt.plot([1, 2, 3], label='Line 1')
line2, = plt.plot([3, 2, 1], label='Line 2')

leg1 = plt.legend(handles=[line1], loc='upper left')
plt.gca().add_artist(leg1)
leg2 = plt.legend(handles=[line2], loc='upper right')

plt.show()
This code plots two lines and adds two separate legends for each line at different locations.
Execution Table
StepActionPlot ElementsLegend CreatedLegend LocationResult
1Plot line1line1 plottedNoneNoneLine 1 appears on plot
2Plot line2line1, line2 plottedNoneNoneLine 2 appears on plot
3Create legend for line1line1, line2 plottedleg1upper leftFirst legend added
4Add first legend to axesline1, line2 plottedleg1upper leftFirst legend visible
5Create legend for line2line1, line2 plottedleg1, leg2upper left, upper rightBoth legends visible
6Show plotline1, line2 plottedleg1, leg2upper left, upper rightPlot displayed with two legends
💡 Plot displayed with both legends visible at specified locations.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
line1NoneLine1 objectLine1 objectLine1 objectLine1 objectLine1 object
line2NoneNoneLine2 objectLine2 objectLine2 objectLine2 object
leg1NoneNoneNoneLegend1 objectLegend1 objectLegend1 object
leg2NoneNoneNoneNoneLegend2 objectLegend2 object
Key Moments - 2 Insights
Why do we need to call add_artist() for the first legend?
Because creating the second legend replaces the first by default, add_artist() keeps the first legend visible as a separate artist (see step 4 in execution_table).
What happens if we don't assign legends to variables?
We can't add them separately or control their positions easily, so only one legend might appear (refer to steps 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the legend location for leg1?
Alower left
Bupper left
Cupper right
Dlower right
💡 Hint
Check the 'Legend Location' column at step 4 in execution_table.
At which step is the second legend (leg2) created?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for legend creation steps.
If we omit plt.gca().add_artist(leg1), what happens to the first legend?
AIt moves to the bottom
BIt still shows up normally
CIt disappears when the second legend is added
DIt overlaps with the second legend
💡 Hint
Refer to key_moments about add_artist() and step 4 in execution_table.
Concept Snapshot
Multiple legends in matplotlib:
- Plot lines and save their handles.
- Create first legend with plt.legend(handles=[...], loc=...).
- Use plt.gca().add_artist() to keep first legend.
- Create second legend similarly.
- Show plot with both legends visible.
Full Transcript
This example shows how to add multiple legends to a matplotlib plot. First, we plot two lines and save their references. Then, we create the first legend for the first line and add it to the axes using add_artist() to keep it visible. Next, we create the second legend for the second line. Finally, we display the plot with both legends visible at different locations. This method prevents the second legend from replacing the first. Variables like line1, line2, leg1, and leg2 track the plot elements and legends. Key points include the need for add_artist() to keep multiple legends and assigning legends to variables for control.