Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the module needed for plotting.
Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing numpy or pandas instead of matplotlib.pyplot
Using seaborn which is a different plotting library
✗ Incorrect
We import matplotlib.pyplot as plt to create plots and legends.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the label text
Using a variable name without defining it
✗ Incorrect
The label must be a string, so it needs quotes around it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not defined
Using plural form when the variable is singular
✗ Incorrect
The variable created is custom_line, so it must be used in the legend handles list.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using colors without quotes
Mixing up the order of colors and labels
✗ Incorrect
The first custom line is blue and the second is green, so use 'blue' and 'green' strings.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using marker characters that do not match the shape labels
Forgetting quotes around marker characters
✗ Incorrect
The markers 'o', 's', and '^' represent circle, square, and triangle shapes respectively.