Challenge - 5 Problems
Legend Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
The 'loc' parameter controls the legend's position inside the plot area.
✗ Incorrect
The 'loc' parameter in plt.legend() specifies the legend's position. 'upper left' places it at the top-left corner inside the plot.
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
facecolor sets the background color of the legend box, fontsize controls text size.
✗ Incorrect
The fontsize parameter sets the legend text size to 14. facecolor='lightgray' sets the background color, and edgecolor='black' adds a black border. Text color defaults to black.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
bbox_to_anchor moves the legend outside the plot area.
✗ Incorrect
loc='center left' with bbox_to_anchor=(1, 0.5) places the legend outside the plot on the right center. shadow=True adds a shadow, framealpha=0.5 makes the background semi-transparent.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Legend needs labels to show entries.
✗ Incorrect
Without labels in plt.plot(), plt.legend() has no text to display, so the legend does not appear.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use 'prop' dictionary to style font properties including color and size.
✗ Incorrect
frameon=False removes the legend frame. The 'prop' parameter accepts a dictionary for font properties like size and color. labelcolor is not a valid parameter for plt.legend.