Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a location string that doesn't exist.
Forgetting quotes around the location string.
✗ Incorrect
The 'loc' parameter controls legend placement. 'upper right' places it at the top right corner.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper right' places legend inside the plot.
Not using bbox_to_anchor for outside placement.
✗ Incorrect
Using 'center right' with bbox_to_anchor=(1, 0.5) places the legend centered outside the right edge.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'center lower' which is invalid.
Using 'bottom center' which is not recognized.
✗ Incorrect
'lower center' is the correct string for placing the legend at the bottom center.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping bbox_to_anchor coordinates.
Using a location that doesn't match bbox_to_anchor.
✗ Incorrect
Using 'upper left' with bbox_to_anchor=(0, 1) places the legend outside the upper left corner with smaller font.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shadow=False disables shadow.
Incorrect bbox_to_anchor coordinates.
✗ Incorrect
Setting loc='lower center', bbox_to_anchor=(0.5, 0), and shadow=True places the legend outside bottom center with shadow.