Bird
Raised Fist0
Matplotlibdata~20 mins

Legend placement options 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 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.

Practice

(1/5)
1. What does the loc parameter control in a matplotlib legend?
easy
A. The type of plot displayed
B. The color of the legend box
C. The font size of the legend text
D. The position of the legend inside or outside the plot area

Solution

  1. Step 1: Understand the role of loc in legend placement

    The loc parameter specifies where the legend appears relative to the plot, such as 'upper right' or 'lower left'.
  2. Step 2: Differentiate from other legend properties

    Other properties like color or font size are controlled by different parameters, not loc.
  3. Final Answer:

    The position of the legend inside or outside the plot area -> Option D
  4. Quick Check:

    Legend position = loc [OK]
Hint: Remember: loc sets legend position, not style [OK]
Common Mistakes:
  • Confusing loc with color or font size
  • Thinking loc controls plot type
  • Assuming loc changes data points
2. Which of the following is the correct syntax to place a legend at the lower center outside the plot using loc and bbox_to_anchor?
easy
A. plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1))
B. plt.legend(loc='center lower', bbox_to_anchor=(0.5, -0.1))
C. plt.legend(location='lower center', bbox_to_anchor=(0.5, -0.1))
D. plt.legend(loc='lower center', bbox_to_anchor=[0.5, -0.1])

Solution

  1. Step 1: Check correct parameter names and values

    The parameter for legend position is loc, and the value 'lower center' is valid. The bbox_to_anchor expects a tuple for coordinates.
  2. Step 2: Identify syntax errors in options

    plt.legend(loc='center lower', bbox_to_anchor=(0.5, -0.1)) uses 'center lower' which is invalid. plt.legend(location='lower center', bbox_to_anchor=(0.5, -0.1)) uses 'location' instead of 'loc'. plt.legend(loc='lower center', bbox_to_anchor=[0.5, -0.1]) uses a list instead of a tuple for bbox_to_anchor.
  3. Final Answer:

    plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1)) -> Option A
  4. Quick Check:

    Correct loc and tuple bbox_to_anchor = plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1)) [OK]
Hint: Use loc='position' and bbox_to_anchor=(x, y) tuple [OK]
Common Mistakes:
  • Using wrong parameter name 'location' instead of 'loc'
  • Swapping order in loc value like 'center lower'
  • Passing list instead of tuple for bbox_to_anchor
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
plt.show()
medium
A. Legend appears inside the plot at upper left corner
B. Legend appears outside the plot at the upper left corner
C. Legend appears outside the plot at the top-right corner
D. Code raises an error due to invalid bbox_to_anchor

Solution

  1. Step 1: Understand loc and bbox_to_anchor interaction

    The loc='upper left' sets the legend's anchor point to its upper left corner, and bbox_to_anchor=(1,1) places that anchor point at the top-right corner of the axes.
  2. Step 2: Determine legend position

    With the legend's upper left anchored at the plot's top-right corner (1,1), the legend extends to the right, appearing outside the plot at the top-right corner.
  3. Final Answer:

    Legend appears outside the plot at the top-right corner -> Option C
  4. Quick Check:

    loc='upper left' + bbox_to_anchor=(1,1) = outside top-right [OK]
Hint: bbox_to_anchor moves legend outside; loc sets anchor point [OK]
Common Mistakes:
  • Assuming legend stays inside plot
  • Confusing loc and bbox_to_anchor roles
  • Expecting an error from bbox_to_anchor values
4. Identify the error in this code that tries to place the legend outside the plot at the bottom right:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Data')
plt.legend(loc='bottom right', bbox_to_anchor=(1, 0))
plt.show()
medium
A. Label parameter is incorrect
B. Invalid loc value 'bottom right'
C. Missing plt.show() call
D. bbox_to_anchor must be a list, not a tuple

Solution

  1. Step 1: Check valid loc values

    Matplotlib does not recognize 'bottom right' as a valid loc string. The correct value is 'lower right'.
  2. Step 2: Verify other parameters

    bbox_to_anchor accepts tuples, plt.show() is present, and label syntax is correct.
  3. Final Answer:

    Invalid loc value 'bottom right' -> Option B
  4. Quick Check:

    loc must be valid string like 'lower right' [OK]
Hint: Use exact loc strings like 'lower right', not 'bottom right' [OK]
Common Mistakes:
  • Using incorrect loc names
  • Thinking bbox_to_anchor needs list
  • Forgetting plt.show()
5. You want to place a legend outside the plot on the right side, vertically centered. Which combination of loc and bbox_to_anchor achieves this?
hard
A. loc='center left', bbox_to_anchor=(1, 0.5)
B. loc='center right', bbox_to_anchor=(1, 0.5)
C. loc='center left', bbox_to_anchor=(0, 0.5)
D. loc='upper right', bbox_to_anchor=(1, 0.5)

Solution

  1. Step 1: Understand legend anchor and position

    To place the legend outside on the right, the anchor point should be on the left side of the legend box, so it aligns just outside the plot.
  2. Step 2: Choose correct loc and bbox_to_anchor

    loc='center left' anchors the legend's left center, and bbox_to_anchor=(1, 0.5) places it just outside the plot on the right, vertically centered.
  3. Final Answer:

    loc='center left', bbox_to_anchor=(1, 0.5) -> Option A
  4. Quick Check:

    Anchor left + bbox at right center = legend outside right [OK]
Hint: Anchor legend left side to place it outside right [OK]
Common Mistakes:
  • Using loc='center right' which anchors wrong side
  • Setting bbox_to_anchor x=0 to place outside right
  • Using upper right loc for vertical center
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