0
0
Matplotlibdata~10 mins

Legend placement and styling 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 a legend to the plot.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.[1]()
Drag options to blanks, or click blank then click option'
Atitle
Blegend
Cshow
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.legend()
Forgetting to add label in plot() before calling legend()
2fill in blank
medium

Complete the code to place the legend in the upper left corner.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'upper left'
B'upper right'
C'lower left'
D'center'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper right' instead of 'upper left'
Not using quotes around the location string
3fill in blank
hard

Fix the error in the code to set the legend font size to 14.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(fontsize=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'14pt'
B'large'
C14
D14.0
Attempts:
3 left
💡 Hint
Common Mistakes
Passing font size as a string with units like '14pt'
Using a float instead of an integer
4fill in blank
hard

Fill both blanks to place the legend at center left and set a shadow.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc=[1], shadow=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A'center left'
B'upper right'
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper right' instead of 'center left'
Setting shadow to False or a string
5fill in blank
hard

Fill all three blanks to create a legend with a title, placed at the lower center, and with fancy box style.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(title=[1], loc=[2], fancybox=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A'My Legend'
B'lower center'
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the title string
Using incorrect location strings
Setting fancybox to False or a string