Bird
Raised Fist0
Matplotlibdata~10 mins

Multiple legends in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the first legend to the plot.

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Alegend
Btitle
Cxlabel
Dylabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using title() instead of legend().
Forgetting to call any function to show the legend.
2fill in blank
medium

Complete the code to create a second legend for the scatter points.

Matplotlib
import matplotlib.pyplot as plt

line1, = plt.plot([1, 2, 3], label='Line 1')
line2, = plt.plot([3, 2, 1], label='Line 2')
scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points')
plt.legend(handles=[line1, line2])
plt.[1](handles=[scatter], loc='upper right')
plt.show()
Drag options to blanks, or click blank then click option'
Aylabel
Btitle
Cxlabel
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using title() or other functions instead of legend().
Not specifying the handles parameter for the second legend.
3fill in blank
hard

Fix the error in the code to correctly add two legends to the plot.

Matplotlib
import matplotlib.pyplot as plt

line1, = plt.plot([1, 2, 3], label='Line 1')
line2, = plt.plot([3, 2, 1], label='Line 2')
scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points')
leg1 = plt.legend(handles=[line1, line2], loc='upper left')
plt.add_artist([1])
plt.legend(handles=[scatter], loc='upper right')
plt.show()
Drag options to blanks, or click blank then click option'
Aleg1
Bscatter
Cline1
Dline2
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to add the scatter object instead of the legend.
Not adding the first legend as an artist before the second legend.
4fill in blank
hard

Fill both blanks to create two legends and add the first legend as an artist.

Matplotlib
import matplotlib.pyplot as plt

line1, = plt.plot([1, 2, 3], label='Line 1')
line2, = plt.plot([3, 2, 1], label='Line 2')
scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points')
leg1 = plt.[1](handles=[line1, line2], loc='upper left')
plt.[2](leg1)
plt.legend(handles=[scatter], loc='upper right')
plt.show()
Drag options to blanks, or click blank then click option'
Alegend
Badd_artist
Ctitle
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using title() or xlabel() instead of add_artist().
Not adding the first legend as an artist before the second legend.
5fill in blank
hard

Fill all three blanks to create two legends with different locations and add the first legend as an artist.

Matplotlib
import matplotlib.pyplot as plt

line1, = plt.plot([1, 2, 3], label='Line 1')
line2, = plt.plot([3, 2, 1], label='Line 2')
scatter = plt.scatter([1, 2, 3], [3, 2, 1], label='Points')
leg1 = plt.[1](handles=[line1, line2], loc=[2])
plt.[3](leg1)
plt.legend(handles=[scatter], loc='upper right')
plt.show()
Drag options to blanks, or click blank then click option'
Alegend
B'upper left'
Cadd_artist
D'lower right'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong location strings or forgetting quotes.
Not adding the first legend as an artist.

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

  1. Step 1: Understand the purpose of legends

    Legends help identify what each line or marker in a plot represents.
  2. 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.
  3. Final Answer:

    To label different groups or data sets separately on the same plot -> Option C
  4. 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

  1. Step 1: Identify how legends are added

    Legends are usually added with plt.legend() or ax.legend().
  2. 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().
  3. Final Answer:

    ax.add_artist() -> Option A
  4. Quick Check:

    Keep first legend visible = add_artist() [OK]
Hint: Use add_artist() to keep first legend visible [OK]
Common Mistakes:
  • Using plt.legend() twice without add_artist()
  • Calling plt.show() to fix legends
  • Trying to set title to fix legends
3. What will the following code display?
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()
medium
A. Two legends: 'Line 1' at upper left and 'Line 2' at upper right
B. Only one legend showing 'Line 2' at upper right
C. Only one legend showing 'Line 1' at upper left
D. No legends displayed

Solution

  1. Step 1: Analyze legend creation

    The first legend is created for 'Line 1' and added as an artist to keep it visible.
  2. Step 2: Analyze second legend addition

    The second legend is created for 'Line 2' and placed at upper right. Both legends appear separately.
  3. Final Answer:

    Two legends: 'Line 1' at upper left and 'Line 2' at upper right -> Option A
  4. Quick Check:

    add_artist() + second legend = two legends [OK]
Hint: add_artist() keeps first legend when adding second [OK]
Common Mistakes:
  • Expecting only one legend to show
  • Forgetting to add_artist() for first legend
  • Placing both legends at same location
4. Identify the error in this code that tries to add two legends:
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()
medium
A. Syntax error due to missing parentheses
B. Second legend call overwrites the first, so only one legend shows
C. Plot lines are not labeled correctly
D. The plot will not display because plt.show() is missing

Solution

  1. Step 1: Understand legend behavior

    Calling ax.legend() twice replaces the previous legend each time.
  2. Step 2: Identify why only one legend appears

    Without using ax.add_artist(), the first legend is removed when the second is added.
  3. Final Answer:

    Second legend call overwrites the first, so only one legend shows -> Option B
  4. Quick Check:

    Multiple legend calls overwrite without add_artist() [OK]
Hint: Use add_artist() to keep multiple legends visible [OK]
Common Mistakes:
  • Assuming multiple legend() calls stack automatically
  • Thinking missing plt.show() causes no plot
  • Believing labels cause syntax errors
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

  1. Step 1: Understand legend placement

    Legends placed at different loc positions avoid overlap visually.
  2. Step 2: Use add_artist() to keep multiple legends

    Adding the first legend as an artist keeps it visible when adding others.
  3. 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.
  4. Final Answer:

    Create first legend, add it with ax.add_artist(), then create second and third legends with different loc values -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard