What is the purpose of creating custom legend entries in matplotlib?
Custom legend entries allow you to add specific labels and symbols to the legend that may not directly correspond to plotted data, helping to explain or highlight important information clearly.
Click to reveal answer
beginner
Which matplotlib class is commonly used to create custom legend handles?
The <code>matplotlib.patches.Patch</code> class is often used to create custom legend handles with specific colors and labels.
Click to reveal answer
intermediate
How do you add a custom legend entry without plotting a new line or marker?
You create a custom handle (like a Patch or Line2D object) and pass it along with a label to the legend() function using the handles and labels parameters.
Click to reveal answer
intermediate
What is the role of Line2D in custom legend entries?
Line2D can be used to create custom line or marker styles for legend entries, especially when you want to show a line or marker that is not part of the plotted data.
Click to reveal answer
beginner
True or False: Custom legend entries can only represent colors, not shapes or line styles.
False. Custom legend entries can represent colors, shapes, line styles, and markers by using appropriate matplotlib objects like Patch or Line2D.
Click to reveal answer
Which parameter do you use to specify custom legend handles in matplotlib?
Amarkers
Blabels
Ccolors
Dhandles
✗ Incorrect
The handles parameter is used to pass custom legend entries (like patches or lines) to the legend.
What matplotlib object would you use to create a colored box for a custom legend entry?
APatch
BLine2D
CFigure
DAxes
✗ Incorrect
Patch objects create colored shapes like boxes or rectangles for legend entries.
If you want to add a custom legend entry showing a dashed line, which object is best to use?
APatch
BText
CLine2D
DScatter
✗ Incorrect
Line2D can create custom line styles such as dashed lines for legend entries.
Can you add a legend entry that does not correspond to any plotted data?
ANo, legend entries must match plotted data
BYes, by creating custom handles
COnly if you plot invisible data
DOnly with special matplotlib plugins
✗ Incorrect
You can add custom legend entries by creating handles manually without plotting data.
Which function is used to display the legend in matplotlib?
Aplt.legend()
Bplt.plot()
Cplt.show()
Dplt.title()
✗ Incorrect
plt.legend() displays the legend on the plot.
Explain how to create and add a custom legend entry in matplotlib without plotting new data.
Think about creating a shape or line object and passing it to legend.
You got /3 concepts.
Describe the difference between using Patch and Line2D for custom legend entries.
Consider what visual element you want to show in the legend.
You got /3 concepts.
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
Step 1: Understand legend purpose
Legends explain what different plot elements represent.
Step 2: Identify custom legend role
Custom legend entries add labels for elements not labeled automatically.
Final Answer:
To add labels for plot elements that are not automatically labeled -> Option C
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
Step 1: Check Line2D constructor
Line2D requires x and y data points as first two arguments.
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.
Final Answer:
Line2D([0], [0], color='red', label='Red Line') -> Option A
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
Step 1: Analyze plot and legend calls
Plot draws a blue line but legend uses only custom_line handle with label 'Custom Red Line'.
Step 2: Understand legend content
Legend shows only what is passed: one red line with label 'Custom Red Line'.
Final Answer:
A legend with one label: 'Custom Red Line' shown in red -> Option D
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
Step 1: Check handles argument type
plt.legend expects handles as a list or iterable, not a single object.
Step 2: Identify fix
Wrap patch in a list: handles=[patch] to fix the error.
Final Answer:
The 'handles' argument should be a list, not a single Patch object -> Option B
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?