0
0
Matplotlibdata~10 mins

Legend placement options 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 place the legend at the upper right 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 right'
B'lower left'
C'center left'
D'best'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a location string that doesn't exist.
Forgetting quotes around the location string.
2fill in blank
medium

Complete the code to place the legend outside the plot on the right side.

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

Fix the error in the code to correctly place the legend at the lower center.

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'lower center'
B'center lower'
C'bottom center'
D'center bottom'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'center lower' which is invalid.
Using 'bottom center' which is not recognized.
4fill in blank
hard

Fill both blanks to place the legend outside the upper left corner with a smaller font size.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc=[1], bbox_to_anchor=[2], fontsize=8)
plt.show()
Drag options to blanks, or click blank then click option'
A'upper left'
B'lower right'
C(0, 1)
D(1, 0)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping bbox_to_anchor coordinates.
Using a location that doesn't match bbox_to_anchor.
5fill in blank
hard

Fill all three blanks to place the legend at the center bottom outside the plot with a shadow effect.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc=[1], bbox_to_anchor=[2], shadow=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A'lower center'
B(0.5, 0)
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using shadow=False disables shadow.
Incorrect bbox_to_anchor coordinates.