Bird
Raised Fist0
Matplotlibdata~20 mins

Legend outside the plot in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of placing a legend outside the plot in matplotlib?
easy
A. To keep the data area clear and improve readability
B. To make the plot smaller
C. To hide the legend from view
D. To change the plot colors automatically

Solution

  1. Step 1: Understand legend placement

    Legends inside the plot can cover data points, making the plot hard to read.
  2. Step 2: Effect of placing legend outside

    Placing the legend outside keeps the data visible and the plot clear.
  3. Final Answer:

    To keep the data area clear and improve readability -> Option A
  4. Quick Check:

    Legend outside = clear data area [OK]
Hint: Legend outside keeps plot data visible and neat [OK]
Common Mistakes:
  • Thinking legend outside hides it
  • Assuming it changes plot size automatically
  • Believing it changes plot colors
2. Which of the following is the correct way to place a legend outside the plot on the right using bbox_to_anchor and loc?
easy
A. plt.legend(loc='upper left', bbox_to_anchor=(-1, 1))
B. plt.legend(loc='center', bbox_to_anchor=(0, 0))
C. plt.legend(loc='lower left', bbox_to_anchor=(0.5, 0.5))
D. plt.legend(loc='upper right', bbox_to_anchor=(1.05, 1))

Solution

  1. Step 1: Understand bbox_to_anchor coordinates

    The tuple (1.05, 1) places the legend just outside the right edge at the top.
  2. Step 2: Match loc with bbox_to_anchor

    Using loc='upper right' anchors the legend's upper right corner to the point (1.05, 1).
  3. Final Answer:

    plt.legend(loc='upper right', bbox_to_anchor=(1.05, 1)) -> Option D
  4. Quick Check:

    Right outside = bbox_to_anchor > 1 on x [OK]
Hint: Use bbox_to_anchor >1 on x with loc='upper right' for right outside [OK]
Common Mistakes:
  • Using bbox_to_anchor inside plot area (less than 1)
  • Mismatching loc and bbox_to_anchor
  • Using negative bbox_to_anchor values incorrectly
3. What will be the effect of the following code snippet?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
medium
A. Legend appears outside the plot on the right center
B. Legend appears inside the plot at center left
C. Legend appears below the plot
D. Code will raise an error

Solution

  1. Step 1: Analyze bbox_to_anchor coordinates

    The point (1, 0.5) is at the right edge horizontally and center vertically.
  2. Step 2: Understand loc='center left'

    The legend's center left side is anchored at (1, 0.5), placing it just outside the plot on the right center.
  3. Final Answer:

    Legend appears outside the plot on the right center -> Option A
  4. Quick Check:

    loc='center left' + bbox_to_anchor=(1,0.5) = right outside center [OK]
Hint: loc='center left' with bbox_to_anchor=(1,0.5) puts legend right outside center [OK]
Common Mistakes:
  • Thinking legend is inside plot
  • Confusing loc and bbox_to_anchor roles
  • Expecting legend below plot
4. Identify the error in this code that tries to place the legend outside the plot on the right:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Data')
plt.legend(loc='outer_right', bbox_to_anchor=(1.05, 1))
plt.show()
medium
A. Missing plt.tight_layout() causes overlap
B. bbox_to_anchor coordinates are out of range
C. Invalid loc value 'outer_right' causes an error
D. No label provided for the plot

Solution

  1. Step 1: Check loc parameter validity

    Matplotlib does not support 'outer_right' as a valid loc string; valid locs include 'upper right', 'center left', etc.
  2. Step 2: Effect of invalid loc

    Using 'outer_right' causes a ValueError and the legend will not display.
  3. Final Answer:

    Invalid loc value 'outer_right' causes an error -> Option C
  4. Quick Check:

    loc must be valid string like 'upper right' [OK]
Hint: Use only valid loc strings like 'upper right', not 'outer_right' [OK]
Common Mistakes:
  • Using invalid loc strings
  • Ignoring need for plt.tight_layout()
  • Forgetting to add label to plot
5. You want to place a legend outside the plot on the right without overlapping the plot or cutting off the legend text. Which combination of commands is best to achieve this?
hard
A. plt.legend(loc='upper right', bbox_to_anchor=(0.5, 0.5)); plt.show()
B. plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)); plt.tight_layout()
C. plt.legend(loc='lower left', bbox_to_anchor=(-0.5, 0)); plt.tight_layout()
D. plt.legend(loc='center', bbox_to_anchor=(1.5, 1.5)); plt.show()

Solution

  1. Step 1: Choose correct legend position

    loc='center left' with bbox_to_anchor=(1, 0.5) places the legend just outside the right center of the plot.
  2. Step 2: Use plt.tight_layout() to avoid overlap

    Calling plt.tight_layout() adjusts the plot layout to prevent the legend from overlapping or being cut off.
  3. Final Answer:

    plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)); plt.tight_layout() -> Option B
  4. Quick Check:

    Right outside + tight_layout = no overlap [OK]
Hint: Use bbox_to_anchor outside + plt.tight_layout() to avoid overlap [OK]
Common Mistakes:
  • Not using plt.tight_layout() causing overlap
  • Placing legend inside plot by wrong bbox_to_anchor
  • Using bbox_to_anchor with negative values incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard