Step 1: Check the shape of ax from plt.subplots(2, 2)
ax is a 2x2 array of axes, not a single axes object.
Step 2: Understand why ax.plot() fails
Calling plot() on an array causes an AttributeError because arrays don't have plot method.
Final Answer:
ax is an array, so ax.plot() causes an error -> Option C
Quick Check:
Array of axes needs indexing before plot() [OK]
Hint: Index axes array before plotting when multiple subplots exist [OK]
Common Mistakes:
Trying to plot on axes array directly
Assuming plt.subplots(2,2) returns single axes
Ignoring need to index axes for plotting
5. You want to create a figure with 3 subplots stacked vertically and set the figure size to 8 inches wide and 12 inches tall. Which code correctly does this?
hard
A. fig, axes = plt.subplots(1, 3, figsize=(12, 8))
B. fig, axes = plt.subplots(3, figsize=(8, 12))
C. fig, axes = plt.subplots(3, 1, size=(8, 12))
D. fig, axes = plt.subplots(3, 1, figsize=(8, 12))
Solution
Step 1: Determine subplot layout for vertical stacking
3 subplots stacked vertically means 3 rows and 1 column.
Step 2: Set figure size correctly
Use figsize=(width, height) with width=8 and height=12 inches.
Final Answer:
fig, axes = plt.subplots(3, 1, figsize=(8, 12)) -> Option D
Quick Check:
Rows=3, cols=1, figsize=(8,12) [OK]
Hint: Use figsize=(width, height) and rows, columns order [OK]
Common Mistakes:
Swapping rows and columns
Using wrong keyword 'size' instead of 'figsize'
Confusing width and height order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.