0
0
Matplotlibdata~20 mins

Legend placement options in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Legend Placement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the legend position in this plot?

Consider this matplotlib code snippet that plots two lines and adds a legend with loc='upper left'. What will be the position of the legend?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
plt.plot([1, 2, 3], [6, 5, 4], label='Line 2')
plt.legend(loc='upper left')
plt.show()
AThe legend appears at the bottom center inside the plot.
BThe legend appears outside the plot area on the right side.
CThe legend appears inside the plot area at the top-left corner.
DThe legend appears outside the plot area at the top.
Attempts:
2 left
💡 Hint

Check the meaning of loc='upper left' in matplotlib legend placement.

Predict Output
intermediate
2:00remaining
What happens with loc='best' in legend?

Given this code, what does loc='best' do for the legend placement?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9], label='Squares')
plt.plot([1, 2, 3], [1, 2, 3], label='Linear')
plt.legend(loc='best')
plt.show()
AAlways places the legend at the center of the plot.
BPlaces the legend outside the plot area at the bottom.
CRaises an error because 'best' is not a valid location.
DAutomatically places the legend in the location with least overlap with data.
Attempts:
2 left
💡 Hint

Think about what 'best' means for legend placement.

🔧 Debug
advanced
2:00remaining
Why does this legend placement code raise an error?

Examine the code below. Why does it raise an error when trying to place the legend?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1], label='Descending')
plt.legend(loc='topright')
plt.show()
ABecause 'topright' is not a valid legend location string in matplotlib.
BBecause the plot has no labels to create a legend.
CBecause the legend must be placed outside the plot area only.
DBecause the plot command is missing a color argument.
Attempts:
2 left
💡 Hint

Check the valid location strings for legend placement in matplotlib.

data_output
advanced
2:00remaining
How many legend entries appear with multiple plots and labels?

Given this code with multiple plots and labels, how many entries will the legend show?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9], label='Squares')
plt.plot([1, 2, 3], [1, 2, 3], label='Linear')
plt.plot([1, 2, 3], [9, 4, 1])  # No label
plt.legend(loc='lower right')
plt.show()
A2 entries: Squares and Linear only.
B1 entry: Only Squares.
C3 entries: Squares, Linear, and the unlabeled plot.
DNo entries because one plot has no label.
Attempts:
2 left
💡 Hint

Only plots with labels appear in the legend.

🚀 Application
expert
3:00remaining
Which legend placement code places the legend outside the plot on the right?

You want to place the legend outside the plot area on the right side. Which code snippet achieves this?

Aplt.legend(loc='upper right')
Bplt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
Cplt.legend(loc='best')
Dplt.legend(loc='lower left', bbox_to_anchor=(0, 0))
Attempts:
2 left
💡 Hint

Use bbox_to_anchor with loc to place legend outside plot.