Complete the code to import the module needed for plotting.
import [1] as plt
We import matplotlib.pyplot as plt to create plots and legends.
Complete the code to create a simple line plot with label 'Line 1'.
plt.plot([1, 2, 3], [4, 5, 6], label=[1]) plt.legend() plt.show()
The label must be a string, so it needs quotes around it.
Fix the error in the code to create a custom legend entry using Line2D.
from matplotlib.lines import Line2D custom_line = Line2D([0], [0], color='red', lw=4) plt.legend(handles=[[1]]) plt.show()
The variable created is custom_line, so it must be used in the legend handles list.
Fill both blanks to create two custom legend entries with different colors and labels.
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()
The first custom line is blue and the second is green, so use 'blue' and 'green' strings.
Fill all three blanks to create a custom legend with shapes and labels.
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()
The markers 'o', 's', and '^' represent circle, square, and triangle shapes respectively.
