You want to plot three lines on the same graph using matplotlib. Which code pattern correctly plots all three lines with different colors?
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [1, 4, 9, 16] y2 = [2, 3, 5, 7] y3 = [3, 5, 7, 11]
Check how multiple lines can be plotted in one call to plt.plot().
Option C uses a single plt.plot() call with x and y pairs for each line and color codes, which is an efficient pattern to plot multiple lines at once.
What will be the output of this matplotlib code snippet?
import matplotlib.pyplot as plt x = [0, 1, 2] for i in range(3): plt.plot(x, [j*(i+1) for j in x]) plt.show()
Consider how the loop changes the y-values for each line.
The loop plots three lines, each with y-values scaled by 1, 2, and 3, resulting in lines with slopes 1, 2, and 3.
What error does this code raise?
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5] plt.plot(x, y) plt.show()
Check if x and y lists have the same length.
Matplotlib requires x and y to have the same length. Here, x has 3 elements but y has 2, causing a ValueError.
You want to plot two lines and add labels and a legend. Which code pattern correctly does this?
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 4, 6] y2 = [1, 3, 5]
Check how labels and legends are added in matplotlib.
Option B uses label in plot calls and calls plt.legend() to show them, plus axis labels, which is the correct pattern.
Which statement best explains why using common plotting patterns is important in data science?
Think about code clarity and efficiency.
Common plotting patterns help reduce repeated code and make it easier for others to understand and maintain visualizations.