Discover how multiple legends can turn a messy chart into a clear story everyone understands!
Why Multiple legends in Matplotlib? - Purpose & Use Cases
Imagine you have a chart with several lines and markers, each representing different data groups. You want to explain these groups clearly, but the default single legend mixes everything together, making it confusing.
Trying to manually create multiple legends by editing the plot repeatedly is slow and frustrating. You might overlap legends or lose track of which label belongs to which data, causing errors and unclear visuals.
Using multiple legends lets you place separate, clear legends for different parts of your plot. This keeps your chart organized and easy to understand without extra manual work.
plt.plot(x1, y1, label='Group A') plt.plot(x2, y2, label='Group B') plt.legend() # One combined legend
leg1 = plt.legend(handles=handles1, labels=labels1, loc='upper left') plt.gca().add_artist(leg1) leg2 = plt.legend(handles=handles2, labels=labels2, loc='lower right')
It enables you to clearly explain complex plots by showing multiple legends, each focused on a specific data group.
In a sales report chart, you can have one legend for product categories and another for sales regions, making it easy for viewers to understand both at once.
Manual single legends can confuse when data groups overlap.
Multiple legends keep explanations clear and organized.
This saves time and improves chart readability.
Practice
matplotlib plot?Solution
Step 1: Understand the purpose of legends
Legends help identify what each line or marker in a plot represents.Step 2: Recognize why multiple legends are needed
When a plot has multiple groups or categories, separate legends make it easier to understand each group distinctly.Final Answer:
To label different groups or data sets separately on the same plot -> Option CQuick Check:
Multiple legends = separate group labels [OK]
- Thinking legends change plot colors
- Confusing legends with saving files
- Assuming legends resize plots
matplotlib method is used to keep the first legend visible when adding a second legend?Solution
Step 1: Identify how legends are added
Legends are usually added withplt.legend()orax.legend().Step 2: Understand how to keep multiple legends
To keep the first legend visible after adding a second, you must add the first legend as an artist usingax.add_artist().Final Answer:
ax.add_artist() -> Option AQuick Check:
Keep first legend visible = add_artist() [OK]
- Using plt.legend() twice without add_artist()
- Calling plt.show() to fix legends
- Trying to set title to fix legends
import matplotlib.pyplot as plt fig, ax = plt.subplots() line1, = ax.plot([1, 2, 3], label='Line 1') line2, = ax.plot([3, 2, 1], label='Line 2') leg1 = ax.legend(handles=[line1], loc='upper left') ax.add_artist(leg1) ax.legend(handles=[line2], loc='upper right') plt.show()
Solution
Step 1: Analyze legend creation
The first legend is created for 'Line 1' and added as an artist to keep it visible.Step 2: Analyze second legend addition
The second legend is created for 'Line 2' and placed at upper right. Both legends appear separately.Final Answer:
Two legends: 'Line 1' at upper left and 'Line 2' at upper right -> Option AQuick Check:
add_artist() + second legend = two legends [OK]
- Expecting only one legend to show
- Forgetting to add_artist() for first legend
- Placing both legends at same location
import matplotlib.pyplot as plt fig, ax = plt.subplots() line1, = ax.plot([1, 2, 3], label='A') line2, = ax.plot([3, 2, 1], label='B') ax.legend(handles=[line1], loc='upper left') ax.legend(handles=[line2], loc='upper right') plt.show()
Solution
Step 1: Understand legend behavior
Callingax.legend()twice replaces the previous legend each time.Step 2: Identify why only one legend appears
Without usingax.add_artist(), the first legend is removed when the second is added.Final Answer:
Second legend call overwrites the first, so only one legend shows -> Option BQuick Check:
Multiple legend calls overwrite without add_artist() [OK]
- Assuming multiple legend() calls stack automatically
- Thinking missing plt.show() causes no plot
- Believing labels cause syntax errors
Solution
Step 1: Understand legend placement
Legends placed at differentlocpositions avoid overlap visually.Step 2: Use add_artist() to keep multiple legends
Adding the first legend as an artist keeps it visible when adding others.Step 3: Create all three legends with distinct locations
Creating legends separately withlocset to 'upper left', 'upper right', and 'lower left' places them without overlap.Final Answer:
Create first legend, add it with ax.add_artist(), then create second and third legends with different loc values -> Option DQuick Check:
add_artist() + distinct loc = multiple legends no overlap [OK]
- Calling legend() multiple times without add_artist()
- Placing all legends at same location causing overlap
- Using single legend for all groups losing clarity
Ready to prove it?
Pass the test and this pattern moves unknown
