What is the purpose of having multiple legends in a matplotlib plot?
Multiple legends help to clearly explain different groups or types of data in the same plot, making it easier to understand complex visuals.
Click to reveal answer
intermediate
How do you create a second legend in matplotlib after the first one?
You create the first legend normally, then save its reference. For the second legend, you use ax.legend() again with different handles and labels, and add it to the axes with ax.add_artist().
Click to reveal answer
intermediate
What does ax.add_artist() do in the context of multiple legends?
It adds the second legend as an artist to the axes, so both legends can appear on the same plot without replacing each other.
Click to reveal answer
beginner
Why might you want to use multiple legends instead of combining all labels into one legend?
Multiple legends allow grouping related items separately, improving clarity when different data types or categories need distinct explanations.
Click to reveal answer
beginner
Which matplotlib object typically holds the legend in a plot?
The legend is an artist object attached to the axes, often returned by ax.legend().
Click to reveal answer
What function do you call to create a legend in matplotlib?
Aax.legend()
Bplt.plot()
Cax.title()
Dplt.show()
✗ Incorrect
The ax.legend() function creates a legend for the plot.
How do you add a second legend to a matplotlib plot without removing the first?
ACall ax.legend() twice and use ax.add_artist() for the second
BCall plt.legend() twice only
CUse plt.show() twice
DCreate two separate plots
✗ Incorrect
You create the first legend normally, then add the second legend as an artist with ax.add_artist().
What does the handles parameter in ax.legend(handles=...) specify?
AThe size of the plot
BThe title of the plot
CWhich plot elements to include in the legend
DThe color of the plot lines
✗ Incorrect
The handles parameter tells matplotlib which plot elements to show in the legend.
Why is it important to use ax.add_artist() when adding a second legend?
ATo change the plot title
BTo keep both legends visible on the same axes
CTo remove the first legend
DTo save the plot as an image
✗ Incorrect
ax.add_artist() adds the second legend without replacing the first.
Which of these is NOT a reason to use multiple legends?
ATo group different data categories separately
BTo explain different plot elements distinctly
CTo improve plot clarity
DTo confuse the viewer
✗ Incorrect
Multiple legends are used to clarify, not confuse, the plot.
Explain how to add multiple legends to a matplotlib plot and why it might be useful.
Think about how to keep both legends visible and why separate explanations help.
You got /5 concepts.
Describe the role of the handles and labels parameters when creating legends in matplotlib.
Handles and labels work together to define legend content.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to use multiple legends in a matplotlib plot?
easy
A. To increase the plot size automatically
B. To change the color of the plot lines
C. To label different groups or data sets separately on the same plot
D. To save the plot as an image file
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 C
Quick Check:
Multiple legends = separate group labels [OK]
Hint: Multiple legends label different groups clearly [OK]
Common Mistakes:
Thinking legends change plot colors
Confusing legends with saving files
Assuming legends resize plots
2. Which matplotlib method is used to keep the first legend visible when adding a second legend?
easy
A. ax.add_artist()
B. plt.show()
C. plt.legend()
D. ax.set_title()
Solution
Step 1: Identify how legends are added
Legends are usually added with plt.legend() or ax.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 using ax.add_artist().
Final Answer:
ax.add_artist() -> Option A
Quick Check:
Keep first legend visible = add_artist() [OK]
Hint: Use add_artist() to keep first legend visible [OK]
5. You want to plot three data groups with separate legends placed at 'upper left', 'upper right', and 'lower left' without overlap. Which approach correctly achieves this?
hard
A. Plot all lines without legends and add a single legend manually
B. Call ax.legend() three times without add_artist(), changing loc each time
C. Use plt.legend() once with all labels combined
D. Create first legend, add it with ax.add_artist(), then create second and third legends with different loc values
Solution
Step 1: Understand legend placement
Legends placed at different loc positions 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 with loc set 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 D
Quick Check:
add_artist() + distinct loc = multiple legends no overlap [OK]
Hint: Add first legend as artist, set loc differently for others [OK]
Common Mistakes:
Calling legend() multiple times without add_artist()
Placing all legends at same location causing overlap
Using single legend for all groups losing clarity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.