0
0
Matplotlibdata~20 mins

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

Choose your learning style9 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.