4. Find the error in this code that tries to plot a line:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
plt.plot(x, y)
plt.show()
medium
A. plt.show() should be called before plt.plot()
B. plt.plot() is missing parentheses
C. x and y lists have different lengths causing an error
D. No error; code runs fine
Solution
Step 1: Check lengths of x and y lists
x has 3 elements, y has 2 elements. They must be the same length to plot points.
Step 2: Understand matplotlib error on length mismatch
Matplotlib raises a ValueError if x and y lengths differ because it cannot pair points properly.
Final Answer:
x and y lists have different lengths causing an error -> Option C
Quick Check:
Equal length x,y needed = A [OK]
Hint: Check x and y lengths match before plotting [OK]
Common Mistakes:
Ignoring length mismatch error
Thinking plt.show() order matters
Assuming missing parentheses cause error
5. You have daily sales data for a week: [100, 120, 115, 130, 125, 140, 150]. How does a line plot help you understand this data better than just looking at numbers?
hard
A. It connects sales points to reveal if sales are increasing or decreasing over days
B. It hides fluctuations and shows only the average sales
C. It shows the exact sales numbers as text on the plot
D. It changes sales data into categories for easier reading
Solution
Step 1: Understand what line plots do with sequential data
Line plots connect points in order, showing how values rise or fall over time.
Step 2: Compare to just numbers
Numbers alone don't show trends clearly; line plots make it easy to see if sales go up or down day by day.
Final Answer:
It connects sales points to reveal if sales are increasing or decreasing over days -> Option A
Quick Check:
Line plot shows trend over time = C [OK]
Hint: Line plots reveal ups and downs in data over time [OK]
Common Mistakes:
Thinking line plots show exact numbers as text
Believing line plots hide fluctuations
Confusing line plots with category charts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.