Challenge - 5 Problems
Custom Legend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
The legend uses only the handles provided explicitly.
✗ Incorrect
The legend is created only with the custom_patch handle, so it shows exactly one legend entry labeled 'Custom Patch'. The original plot line is not included because it is not passed to legend.
❓ data_output
intermediate2: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()
Attempts:
2 left
💡 Hint
Only the handles passed to legend are shown.
✗ Incorrect
The legend is created with handles=[line1, custom_line], so only 'Squares' and 'Custom Point' labels appear.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
Custom legend entries can combine Line2D and Patch objects.
✗ Incorrect
The legend uses a blue Line2D object and a red Patch, so both appear with correct labels.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check if the legend entries have labels.
✗ Incorrect
The Patch was created without a label, so the legend entry is empty and no label is shown.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Markers with no connecting line use linestyle='None'.
✗ Incorrect
Option A correctly sets linestyle='--' for dashed line, linestyle='None' for marker only, and uses Patch for the blue square.