Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Matplotlib - Image Display
Identify the error in this code snippet:
fig, axes = plt.subplots(2, 2)
for i in range(4):
    axes[i].imshow(np.random.rand(5,5))
plt.show()
Aplt.subplots cannot create 2x2 grids
Bimshow requires 3D arrays, so error occurs
Caxes is a 2D array; axes[i] indexing causes error
DNo error; code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check axes shape

    For 2x2 grid, axes is a 2D array (2 rows, 2 columns).
  2. Step 2: Check indexing

    axes[i] tries to index 1D, but axes is 2D, causing an error.
  3. Final Answer:

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

    2D axes require 2D indexing [OK]
Quick Trick: 2D subplot axes need row and column indices [OK]
Common Mistakes:
  • Using 1D indexing on 2D axes array
  • Assuming imshow needs 3D arrays
  • Ignoring axes shape after subplots

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes