Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The 'center left' location places the legend outside the plot on the right side when combined with bbox_to_anchor.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The tuple (1, 0.5) places the legend just outside the right center of the plot.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a set instead of a tuple causes errors.
Passing multiple arguments without parentheses is invalid.
✗ Incorrect
bbox_to_anchor requires a tuple to specify coordinates, so (1, 0.5) is correct.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Using loc='center left' with bbox_to_anchor=(1, 0.5) places the legend outside right center with no overlap.
5fill in blank
hardFill 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'
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.
✗ Incorrect
loc='center left' and bbox_to_anchor=(1, 0.5) place the legend outside right center; title='My Legend' adds a title.