Consider the following Python code using matplotlib to create a sequence of plots. What will be the output shown after running this code?
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title('Sine Wave') plt.show() plt.plot(x, np.cos(x)) plt.title('Cosine Wave') plt.show()
Think about what plt.show() does in matplotlib and how it affects the display of plots.
Each plt.show() call displays the current figure and clears it. So the first plot shows the sine wave, then the second plot shows the cosine wave separately.
Given the code below, how many points are plotted in total across all plots?
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 50) plt.plot(x, x**2) plt.show() plt.plot(x, np.sqrt(x)) plt.show()
Check how many points are generated by np.linspace and how many plots are created.
np.linspace(0, 5, 50) creates 50 points. Each plot uses all 50 points, so total points plotted are 50 + 50 = 100.
You want to tell a story showing data trends over time using three plots: a line plot, a bar chart, and a scatter plot. Which sequence best supports a clear storytelling flow?
Think about introducing the trend first, then details, then individual points.
Starting with a line plot shows the overall trend, followed by a bar chart to compare categories, and ending with a scatter plot to show individual data points supports a clear story.
Examine the code below. What error will it raise when run?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5]) plt.show()
Check if the x and y data lists have the same length.
Matplotlib requires x and y data to have the same length. Here, x has 3 points but y has 2, causing a ValueError.
Which code snippet correctly creates a figure with 3 vertically stacked plots sharing the same x-axis for storytelling?
Consider the layout and which axis should be shared for aligned storytelling.
Option C creates 3 rows (3,1) with shared x-axis, which aligns the plots vertically and shares the x-axis for better comparison.