0
0
Matplotlibdata~20 mins

Legend outside the plot 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 position of the legend in this plot?

Consider the following Python code using matplotlib. What will be the position of the legend relative to the plot?

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
AThe legend is inside the plot area at the bottom left corner.
BThe legend is inside the plot area at the top right corner.
CThe legend is outside the plot area on the right, vertically centered.
DThe legend is below the plot, centered horizontally.
Attempts:
2 left
💡 Hint

Look at the bbox_to_anchor coordinates and the loc parameter.

data_output
intermediate
1:30remaining
How many legend entries will appear?

Given the following code, how many entries will the legend display?

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='A')
plt.plot([3, 2, 1])
plt.plot([2, 2, 2], label='B')
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
plt.show()
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Only lines with a label are shown in the legend.

visualization
advanced
2:00remaining
Identify the correct code to place the legend outside below the plot

Which code snippet correctly places the legend centered below the plot area?

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

Think about the legend anchor point and its position below the plot.

🔧 Debug
advanced
2:00remaining
Why does this legend not appear outside the plot?

Examine the code below. The user wants the legend outside the plot on the right, but it appears inside. Why?

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='X')
plt.legend(loc='right', bbox_to_anchor=(1, 0.5))
plt.show()
Aloc='right' anchors the midpoint of the legend's right side to (1, 0.5), causing it to extend inside the plot; use 'center left' instead.
BThe bbox_to_anchor coordinates are outside the plot, so legend is hidden.
CThe plot has no labels, so legend is empty and not shown.
DThe legend is outside but the figure size is too small to see it.
Attempts:
2 left
💡 Hint

Consider what point of the legend loc='right' places at bbox_to_anchor.

🚀 Application
expert
3:00remaining
How to ensure legend outside plot does not overlap with figure content?

You want to place the legend outside the plot on the right side without overlapping the plot content. Which approach is best?

AUse <code>plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))</code> and adjust <code>plt.subplots_adjust(right=0.75)</code> to shrink plot area.
BUse <code>plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))</code> only, no other adjustments needed.
CIncrease figure size only with <code>plt.figure(figsize=(10,6))</code> without changing legend position.
DPlace legend inside plot with <code>loc='best'</code> to avoid overlap.
Attempts:
2 left
💡 Hint

Think about how to make space for the legend outside the plot area.