0
0
Matplotlibdata~10 mins

Legend outside the plot 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 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'lower right'
B'upper left'
C'center left'
D'best'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'best' places the legend inside the plot area.
Using 'upper left' or 'lower right' places the legend inside the plot.
2fill in blank
medium

Complete the code to move the legend outside the plot using bbox_to_anchor.

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

Fix the error in the code to correctly place the legend outside the plot on the right.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc='center left', bbox_to_anchor=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A(1, 0.5)
B[1, 0.5]
C{1, 0.5}
D1, 0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a set instead of a tuple causes errors.
Passing multiple arguments without parentheses is invalid.
4fill in blank
hard

Fill both blanks to place the legend outside the plot on the right and prevent overlap.

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

Fill all three blanks to place the legend outside the plot on the right, set title, and adjust font size.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc=[1], bbox_to_anchor=[2], title=[3], fontsize=10)
plt.show()
Drag options to blanks, or click blank then click option'
A'center left'
B(1, 0.5)
C'My Legend'
D'upper left'
Attempts:
3 left
💡 Hint
Common Mistakes
Using loc or bbox_to_anchor values that place the legend inside the plot.
Forgetting to provide a string for the title parameter.