Bird
Raised Fist0
Matplotlibdata~20 mins

Legend placement and styling in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Legend Mastery Badge
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 matplotlib code that plots two lines and places a legend. What will be the location of the legend on 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='upper left')
plt.show()
AThe legend appears at the center of the plot area.
BThe legend appears outside the plot area on the right side.
CThe legend appears at the upper left corner of the plot area.
DThe legend appears at the bottom right corner of the plot area.
Attempts:
2 left
💡 Hint
The 'loc' parameter controls the legend's position inside the plot area.
❓ Predict Output
intermediate
2:00remaining
What color and font size will the legend text have?
Given this code snippet, what will be the color and font size of the legend text?
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='Data')
plt.legend(fontsize=14, facecolor='lightgray', edgecolor='black')
plt.show()
AThe legend text will be black with default font size, and the legend box will have a white background with no border.
BThe legend text will be black with font size 14, and the legend box will have a light gray background with a black border.
CThe legend text will be light gray with font size 14, and the legend box will have a black background with a light gray border.
DThe legend text will be white with font size 14, and the legend box will have a light gray background with no border.
Attempts:
2 left
💡 Hint
facecolor sets the background color of the legend box, fontsize controls text size.
❓ visualization
advanced
3:00remaining
Which legend style matches the plot output?
A plot has three lines with a legend placed outside the plot on the right side. The legend has a shadow and a semi-transparent background. Which code snippet produces this legend style?
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='A')
plt.plot([3, 2, 1], label='B')
plt.plot([2, 3, 1], label='C')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), shadow=True, framealpha=0.5)
plt.show()
Aplt.legend(loc='center left', bbox_to_anchor=(1, 0.5), shadow=True, framealpha=0.5)
Bplt.legend(loc='upper right', shadow=True, framealpha=0.5)
Cplt.legend(loc='best', bbox_to_anchor=(1, 1), shadow=False, framealpha=1)
Dplt.legend(loc='lower left', bbox_to_anchor=(0, 0), shadow=True, framealpha=0)
Attempts:
2 left
💡 Hint
bbox_to_anchor moves the legend outside the plot area.
🔧 Debug
advanced
2:00remaining
Why does this legend not appear on the plot?
This code plots two lines and tries to add a legend, but the legend does not show. What is the reason?
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.plot([3, 2, 1])
plt.legend()
plt.show()
AThe plt.show() call is missing, so the plot and legend do not render.
BThe plt.legend() call is missing the loc parameter, so the legend is hidden.
CThe plt.plot() calls are missing color arguments, so the legend cannot be created.
DThe plot lines have no labels, so the legend has nothing to display.
Attempts:
2 left
💡 Hint
Legend needs labels to show entries.
🚀 Application
expert
3:00remaining
How to place a legend inside a plot with custom font and no frame?
You want to place a legend inside the plot at the lower center position. The legend text should be size 12 and blue. The legend box should have no frame or background color. Which code snippet achieves this?
Aplt.legend(loc='lower center', fontsize=12, frameon=False, prop={'size':12, 'color':'blue'})
Bplt.legend(loc='lower center', fontsize=12, framealpha=0, labelcolor='blue')
Cplt.legend(loc='lower center', fontsize=12, frameon=False, prop={'color':'blue'})
Dplt.legend(loc='lower center', fontsize=12, frameon=False, labelcolor='blue')
Attempts:
2 left
💡 Hint
Use 'prop' dictionary to style font properties including color and size.

Practice

(1/5)
1. What does the loc parameter control in a matplotlib legend?
easy
A. The type of plot line used
B. The color of the legend text
C. The size of the plot markers
D. The position of the legend on the plot

Solution

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

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

    Other options control color, size, or line style, but loc only controls position.
  3. Final Answer:

    The position of the legend on the plot -> Option D
  4. Quick Check:

    Legend position = loc [OK]
Hint: Remember: loc means location of legend [OK]
Common Mistakes:
  • Confusing loc with color or font size
  • Thinking loc changes plot markers
  • Assuming loc controls line style
2. Which of the following is the correct syntax to place a legend at the lower left corner in matplotlib?
easy
A. plt.legend(loc='lower left')
B. plt.legend(location='lower left')
C. plt.legend(loc='lower_left')
D. plt.legend(position='lower left')

Solution

  1. Step 1: Identify the correct parameter name

    The correct parameter to set legend position is loc, not location or position.
  2. Step 2: Use the correct string value

    The valid string is 'lower left' with a space, not 'lower_left' with underscore.
  3. Final Answer:

    plt.legend(loc='lower left') -> Option A
  4. Quick Check:

    Correct syntax uses loc='lower left' [OK]
Hint: Use loc='lower left' exactly, no underscores [OK]
Common Mistakes:
  • Using 'location' instead of 'loc'
  • Using underscores instead of spaces
  • Using 'position' parameter which doesn't exist
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line A')
plt.plot([3, 2, 1], label='Line B')
plt.legend(loc='upper center', shadow=True, fontsize='small')
plt.show()
medium
A. Code will raise an error due to invalid fontsize
B. Legend appears at the top center with a shadow and small font
C. Legend appears inside the plot area without shadow
D. Legend appears at the bottom right with default font size

Solution

  1. Step 1: Analyze legend parameters

    The loc='upper center' places the legend at the top center. shadow=True adds a shadow behind the legend box. fontsize='small' sets the font size smaller than default.
  2. Step 2: Confirm no errors in parameters

    All parameters are valid and supported by matplotlib.
  3. Final Answer:

    Legend appears at the top center with a shadow and small font -> Option B
  4. Quick Check:

    loc + shadow + fontsize = top center, shadow, small font [OK]
Hint: Check loc for position, shadow=True adds shadow, fontsize sets text size [OK]
Common Mistakes:
  • Assuming fontsize='small' causes error
  • Confusing loc='upper center' with bottom right
  • Ignoring shadow parameter effect
4. Identify the error in this code snippet for legend placement:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='A')
plt.plot([3, 2, 1], label='B')
plt.legend(loc='bottomright')
plt.show()
medium
A. plt.legend() must be called before plt.plot()
B. Missing label parameter in plot calls
C. The loc value 'bottomright' is invalid; should be 'lower right'
D. plt.show() is missing parentheses

Solution

  1. Step 1: Check the loc parameter value

    The valid location string is 'lower right', not 'bottomright'. 'bottomright' is not recognized by matplotlib.
  2. Step 2: Verify other parts of the code

    Labels are correctly set, plt.legend() can be called after plotting, and plt.show() has parentheses.
  3. Final Answer:

    The loc value 'bottomright' is invalid; should be 'lower right' -> Option C
  4. Quick Check:

    Valid loc strings = 'lower right' not 'bottomright' [OK]
Hint: Use exact loc strings like 'lower right', not 'bottomright' [OK]
Common Mistakes:
  • Using invalid loc strings
  • Thinking plt.legend() must come before plt.plot()
  • Forgetting parentheses in plt.show()
5. You want to place a legend outside the plot on the right side with a title 'Legend', font size 12, and a shadow. Which code snippet achieves this correctly?
hard
A. plt.legend(loc='center right', bbox_to_anchor=(1, 0.5), title='Legend', fontsize='12pt', shadow=True)
B. plt.legend(loc='right side', bbox_to_anchor=(1, 0.5), title='Legend', fontsize=12, shadow=True)
C. plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), title='Legend', fontsize=12, shadow=True)
D. plt.legend(loc='outside right', bbox_to_anchor=(1, 0.5), title='Legend', fontsize=12, shadow=True)

Solution

  1. Step 1: Understand placing legend outside plot

    Using loc='center right' with bbox_to_anchor=(1, 0.5) places the legend outside on the right side centered vertically.
  2. Step 2: Check styling parameters

    Title 'Legend', font size 12 (string with units '12pt'), and shadow=True are correctly set. loc='right side' and loc='outside right' are invalid.
  3. Final Answer:

    plt.legend(loc='center right', bbox_to_anchor=(1, 0.5), title='Legend', fontsize='12pt', shadow=True) -> Option A
  4. Quick Check:

    loc + bbox_to_anchor + title + fontsize + shadow = correct legend outside right [OK]
Hint: Use loc='center right' with bbox_to_anchor=(1, 0.5) for outside right legend [OK]
Common Mistakes:
  • Using invalid loc like 'right side' or 'outside right'
  • Passing fontsize as integer without units
  • Forgetting bbox_to_anchor for outside placement
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