0
0
Matplotlibdata~10 mins

Custom legend entries in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed for plotting.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Aseaborn
Bmatplotlib.pyplot
Cnumpy
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Importing numpy or pandas instead of matplotlib.pyplot
Using seaborn which is a different plotting library
2fill in blank
medium

Complete the code to create a simple line plot with label 'Line 1'.

Matplotlib
plt.plot([1, 2, 3], [4, 5, 6], label=[1])
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Aline1
BLine 1
C'Line 1'
Dlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the label text
Using a variable name without defining it
3fill in blank
hard

Fix the error in the code to create a custom legend entry using Line2D.

Matplotlib
from matplotlib.lines import Line2D
custom_line = Line2D([0], [0], color='red', lw=4)
plt.legend(handles=[[1]])
plt.show()
Drag options to blanks, or click blank then click option'
Acustom_line
Bline
Ccustom_lines
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not defined
Using plural form when the variable is singular
4fill in blank
hard

Fill both blanks to create two custom legend entries with different colors and labels.

Matplotlib
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color=[1], lw=4), Line2D([0], [0], color=[2], lw=4)]
plt.legend(custom_lines, ['Blue line', 'Green line'])
plt.show()
Drag options to blanks, or click blank then click option'
A'blue'
B'red'
C'green'
D'yellow'
Attempts:
3 left
💡 Hint
Common Mistakes
Using colors without quotes
Mixing up the order of colors and labels
5fill in blank
hard

Fill all three blanks to create a custom legend with shapes and labels.

Matplotlib
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color='black', marker=[1], linestyle='None', markersize=10),
                Line2D([0], [0], color='black', marker=[2], linestyle='None', markersize=10),
                Line2D([0], [0], color='black', marker=[3], linestyle='None', markersize=10)]
plt.legend(custom_lines, ['Circle', 'Square', 'Triangle'])
plt.show()
Drag options to blanks, or click blank then click option'
A'o'
B's'
C'^'
D'x'
Attempts:
3 left
💡 Hint
Common Mistakes
Using marker characters that do not match the shape labels
Forgetting quotes around marker characters