Bird
0
0

Identify the error in this code for creating a 2x2 grid of plots:

medium📝 Debug Q14 of 15
Matplotlib - Real-World Visualization Patterns
Identify the error in this code for creating a 2x2 grid of plots:
fig, axes = plt.subplots(2, 2)
for i in range(4):
    axes[i].plot([1, 2, 3])
plt.show()
AThe plot data list is invalid
Bplt.subplots(2, 2) creates only 2 plots, not 4
Cplt.show() is missing
Daxes is a 2D array, so axes[i] indexing causes an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand axes shape from plt.subplots(2, 2)

    axes is a 2x2 numpy array of Axes objects, not a flat list.
  2. Step 2: Identify indexing error

    axes[i] tries to index a 2D array with one index, causing an error. Correct is axes[row, col] or flatten axes first.
  3. Final Answer:

    axes is a 2D array, so axes[i] indexing causes an error -> Option D
  4. Quick Check:

    2D axes need two indices or flatten before looping [OK]
Quick Trick: 2D axes need two indices or flatten before indexing [OK]
Common Mistakes:
  • Assuming axes is 1D array
  • Ignoring error from wrong indexing
  • Thinking plt.show() is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes