0
0
Matplotlibdata~10 mins

Multiple legends in Matplotlib - Interactive Code Practice

Choose your learning style9 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.