0
0
Matplotlibdata~20 mins

Multiple legends in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Legends Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple legends with different labels
What will be the output of this code snippet that creates two legends for two different line plots?
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]

line1, = plt.plot(x, y1, label='Line 1')
line2, = plt.plot(x, y2, label='Line 2')

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

plt.show()
ANo legend appears because plt.legend() is called twice
BOnly one legend appears with both 'Line 1' and 'Line 2' labels
COnly one legend appears with label 'Line 2' on upper right
DTwo legends appear: 'Line 1' on upper left and 'Line 2' on upper right
Attempts:
2 left
💡 Hint
Think about how adding the first legend as an artist affects the second legend call.
🧠 Conceptual
intermediate
1:30remaining
Why add_artist() is needed for multiple legends?
Why do we need to use add_artist() when adding multiple legends in matplotlib?
ABecause matplotlib only supports one legend by default, add_artist() adds the first legend explicitly so the second can be added
BBecause add_artist() automatically merges all legends into one
CBecause add_artist() removes the first legend to add a new one
DBecause add_artist() changes the legend font size
Attempts:
2 left
💡 Hint
Think about how matplotlib handles legends internally.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple legends code
What error will this code produce when trying to add two legends without add_artist()?
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')

plt.legend(loc='upper left')
plt.legend(loc='upper right')

plt.show()
ANo legend appears because second call overwrites the first
BOnly one legend appears at upper right, no error
CSyntaxError due to multiple plt.legend() calls
DRuntimeError: Multiple legends not supported
Attempts:
2 left
💡 Hint
What happens when plt.legend() is called twice without add_artist()?
visualization
advanced
2:30remaining
Visualize multiple legends with different styles
Which option correctly creates two legends with different colors and markers for two scatter plots?
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]

scatter1 = plt.scatter(x, y1, color='red', marker='o', label='Red circles')
scatter2 = plt.scatter(x, y2, color='blue', marker='x', label='Blue crosses')

legend1 = plt.legend(handles=[scatter1], loc='upper left')
plt.gca().add_artist(legend1)
plt.legend(handles=[scatter2], loc='upper right')

plt.show()
AOnly blue crosses legend appears on upper right
BOne legend appears with both red circles and blue crosses combined
CTwo legends appear: red circles on upper left, blue crosses on upper right
DNo legends appear because scatter plots do not support legends
Attempts:
2 left
💡 Hint
Check how handles and add_artist() are used for multiple legends.
🚀 Application
expert
3:00remaining
Create multiple legends for grouped bar chart
You have a grouped bar chart with two groups: 'Men' and 'Women', each with bars for '2019' and '2020'. How do you create two legends: one for gender and one for year?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = ['A', 'B', 'C']
men_2019 = [5, 7, 6]
women_2019 = [6, 9, 5]
men_2020 = [7, 8, 7]
women_2020 = [8, 10, 6]

x = np.arange(len(labels))
width = 0.2

fig, ax = plt.subplots()

bar1 = ax.bar(x - width, men_2019, width, label='Men 2019', color='blue')
bar2 = ax.bar(x, women_2019, width, label='Women 2019', color='orange')
bar3 = ax.bar(x + width, men_2020, width, label='Men 2020', color='green')
bar4 = ax.bar(x + 2*width, women_2020, width, label='Women 2020', color='red')

# Your code to add two legends here

plt.show()
ACreate legend1 with handles [bar1, bar2] labeled 'Men', 'Women'; add_artist(legend1); create legend2 with handles [bar1, bar3] labeled '2019', '2020'
BCreate legend1 with handles [bar1, bar2, bar3, bar4] labeled 'Men', 'Women', '2019', '2020'; no add_artist() needed
CCreate legend1 with handles [bar1, bar3] labeled 'Men', 'Women'; add_artist(legend1); create legend2 with handles [bar2, bar4] labeled '2019', '2020'
DCreate legend1 with handles [bar1, bar2] labeled '2019', '2020'; add_artist(legend1); create legend2 with handles [bar3, bar4] labeled 'Men', 'Women'
Attempts:
2 left
💡 Hint
Group bars by gender and year, then create legends accordingly using add_artist() for the first legend.