Bird
Raised Fist0
Matplotlibdata~20 mins

Custom legend entries in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Legend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What does this code output?
Consider the following matplotlib code that creates a plot and a custom legend entry. What will be the number of legend entries shown?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
custom_patch = mpatches.Patch(color='red', label='Custom Patch')
plt.legend(handles=[custom_patch])
plt.close()
A3
B1
C0
D2
Attempts:
2 left
💡 Hint
The legend uses only the handles provided explicitly.
❓ data_output
intermediate
2:00remaining
What labels appear in the legend?
Given this code snippet, which labels will appear in the legend?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.lines as mlines

line1, = plt.plot([1, 2, 3], [1, 4, 9], label='Squares')
line2, = plt.plot([1, 2, 3], [1, 8, 27], label='Cubes')
custom_line = mlines.Line2D([], [], color='green', marker='o', linestyle='None', label='Custom Point')
plt.legend(handles=[line1, custom_line])
plt.close()
ASquares, Custom Point
BSquares, Cubes, Custom Point
CCubes, Custom Point
DSquares, Cubes
Attempts:
2 left
💡 Hint
Only the handles passed to legend are shown.
❓ visualization
advanced
2:30remaining
Identify the correct legend appearance
Which option shows a legend with two entries: a blue line labeled 'Data' and a red square labeled 'Threshold'?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines

plt.plot([0, 1, 2], [2, 3, 4], color='blue', label='Data')
red_square = mpatches.Patch(color='red', label='Threshold')
plt.legend(handles=[mlines.Line2D([], [], color='blue', label='Data'), red_square])
plt.close()
ALegend with a blue line labeled 'Data' only
BLegend with a red square labeled 'Threshold' only
CLegend with a blue line labeled 'Data' and a red square labeled 'Threshold'
DLegend with no entries
Attempts:
2 left
💡 Hint
Custom legend entries can combine Line2D and Patch objects.
🔧 Debug
advanced
2:00remaining
Why does this custom legend not show the label?
This code tries to add a custom legend entry but the label does not appear. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

plt.plot([1, 2, 3], [3, 2, 1])
custom_patch = mpatches.Patch(color='orange')
plt.legend(handles=[custom_patch])
plt.close()
ALegend requires plt.show() to display labels
BThe plot line must be included in handles to show any legend
CThe color 'orange' is invalid and causes the label to hide
DThe Patch object lacks a label argument, so no label appears
Attempts:
2 left
💡 Hint
Check if the legend entries have labels.
🚀 Application
expert
3:00remaining
Create a custom legend with mixed marker and line styles
You want a legend with three entries: a green dashed line labeled 'Trend', a red circle marker labeled 'Data Point', and a blue square patch labeled 'Region'. Which code snippet correctly creates this custom legend?
A
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches

trend_line = mlines.Line2D([], [], color='green', linestyle='--', label='Trend')
data_point = mlines.Line2D([], [], color='red', marker='o', linestyle='None', label='Data Point')
region_patch = mpatches.Patch(color='blue', label='Region')
plt.legend(handles=[trend_line, data_point, region_patch])
B
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches

trend_line = mlines.Line2D([], [], color='green', linestyle='-', label='Trend')
data_point = mpatches.Circle((0,0), radius=5, color='red', label='Data Point')
region_patch = mpatches.Rectangle((0,0), 1, 1, color='blue', label='Region')
plt.legend(handles=[trend_line, data_point, region_patch])
C
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches

trend_line = mlines.Line2D([], [], color='green', linestyle='--', label='Trend')
data_point = mlines.Line2D([], [], color='red', marker='o', label='Data Point')
region_patch = mpatches.Patch(color='blue', label='Region')
plt.legend(handles=[trend_line, data_point, region_patch])
D
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches

trend_line = mlines.Line2D([], [], color='green', linestyle='--', label='Trend')
data_point = mlines.Line2D([], [], color='red', marker='o', linestyle='-', label='Data Point')
region_patch = mpatches.Patch(color='blue', label='Region')
plt.legend(handles=[trend_line, data_point, region_patch])
Attempts:
2 left
💡 Hint
Markers with no connecting line use linestyle='None'.

Practice

(1/5)
1. What is the main purpose of creating custom legend entries in matplotlib?
easy
A. To change the plot background color
B. To increase the plot size
C. To add labels for plot elements that are not automatically labeled
D. To save the plot as an image file

Solution

  1. Step 1: Understand legend purpose

    Legends explain what different plot elements represent.
  2. Step 2: Identify custom legend role

    Custom legend entries add labels for elements not labeled automatically.
  3. Final Answer:

    To add labels for plot elements that are not automatically labeled -> Option C
  4. Quick Check:

    Custom legend entries = add labels [OK]
Hint: Custom legends label unlabeled plot parts quickly [OK]
Common Mistakes:
  • Confusing legend with plot size or color
  • Thinking legend changes plot background
  • Assuming legend saves the plot
2. Which of the following is the correct way to create a custom legend entry for a red line using matplotlib.lines.Line2D?
easy
A. Line2D([0], [0], color='red', label='Red Line')
B. Line2D(color='red', label='Red Line')
C. Line2D([0,1], [0,1], color='red')
D. Line2D([0], [0], 'red', label='Red Line')

Solution

  1. Step 1: Check Line2D constructor

    Line2D requires x and y data points as first two arguments.
  2. Step 2: Verify correct syntax

    Line2D([0], [0], color='red', label='Red Line') uses [0], [0] for points, color='red', and label='Red Line' correctly.
  3. Final Answer:

    Line2D([0], [0], color='red', label='Red Line') -> Option A
  4. Quick Check:

    Line2D needs x,y points + color + label [OK]
Hint: Line2D needs x and y lists first, then style [OK]
Common Mistakes:
  • Omitting x and y data points
  • Passing color as positional argument
  • Missing label parameter
3. What will be the output legend labels for this code snippet?
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

plt.plot([1, 2, 3], [4, 5, 6], 'b-')
custom_line = Line2D([0], [0], color='red', lw=2)
plt.legend([custom_line], ['Custom Red Line'])
plt.show()
medium
A. A legend with two labels: 'Custom Red Line' and 'blue line'
B. A legend with one label: 'blue line' shown in blue
C. No legend will appear
D. A legend with one label: 'Custom Red Line' shown in red

Solution

  1. Step 1: Analyze plot and legend calls

    Plot draws a blue line but legend uses only custom_line handle with label 'Custom Red Line'.
  2. Step 2: Understand legend content

    Legend shows only what is passed: one red line with label 'Custom Red Line'.
  3. Final Answer:

    A legend with one label: 'Custom Red Line' shown in red -> Option D
  4. Quick Check:

    Legend shows passed handles and labels only [OK]
Hint: Legend shows only handles and labels you pass [OK]
Common Mistakes:
  • Assuming plot lines auto-appear in legend
  • Expecting multiple legend entries without passing handles
  • Thinking legend is empty
4. Identify the error in this code that tries to add a custom legend entry for a green square patch:
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

patch = Patch(color='green', label='Green Square')
plt.legend(handles=patch, labels=['Green Square'])
plt.show()
medium
A. Patch cannot be used for legend entries
B. The 'handles' argument should be a list, not a single Patch object
C. The 'label' parameter is missing in Patch
D. plt.show() must come before plt.legend()

Solution

  1. Step 1: Check handles argument type

    plt.legend expects handles as a list or iterable, not a single object.
  2. Step 2: Identify fix

    Wrap patch in a list: handles=[patch] to fix the error.
  3. Final Answer:

    The 'handles' argument should be a list, not a single Patch object -> Option B
  4. Quick Check:

    Handles must be list or iterable [OK]
Hint: Always pass handles as a list to plt.legend [OK]
Common Mistakes:
  • Passing single object instead of list to handles
  • Ignoring label parameter in Patch
  • Calling plt.show() before plt.legend()
5. You want to create a legend with a blue dashed line labeled 'Trend' and a red circle marker labeled 'Data Point'. Which code correctly creates custom legend entries for this?
hard
A. from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color='blue', lw=2, linestyle='--', label='Trend'), Line2D([0], [0], marker='o', color='red', linestyle='', label='Data Point')] plt.legend(handles=legend_elements)
B. from matplotlib.patches import Patch legend_elements = [Patch(color='blue', label='Trend'), Patch(color='red', label='Data Point')] plt.legend(handles=legend_elements)
C. plt.legend(['Trend', 'Data Point'], loc='best')
D. plt.legend(['blue dashed line', 'red circle'])

Solution

  1. Step 1: Identify correct legend handles

    Blue dashed line and red circle marker require Line2D objects with correct styles.
  2. Step 2: Verify code correctness

    from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color='blue', lw=2, linestyle='--', label='Trend'), Line2D([0], [0], marker='o', color='red', linestyle='', label='Data Point')] plt.legend(handles=legend_elements) creates Line2D with color='blue', linestyle='--' for 'Trend' and marker='o', color='red' for 'Data Point', then passes handles to plt.legend.
  3. Final Answer:

    from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color='blue', lw=2, linestyle='--', label='Trend'), Line2D([0], [0], marker='o', color='red', linestyle='', label='Data Point')] plt.legend(handles=legend_elements) -> Option A
  4. Quick Check:

    Use Line2D with style and marker for custom legend [OK]
Hint: Use Line2D with marker and linestyle for custom legend [OK]
Common Mistakes:
  • Using Patch for line and marker legend
  • Passing only label strings without handles
  • Ignoring linestyle or marker parameters
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