C. AttributeError: 'AxesSubplot' object has no attribute 'plot'
D. IndexError: index 2 is out of bounds for axis 0 with size 2
Solution
Step 1: Check axes shape from plt.subplots(2, 2)
axes is a 2x2 array with indices 0 and 1 for rows and columns.
Step 2: Analyze axes[2, 0] access
Index 2 for rows is out of bounds since max index is 1.
Final Answer:
IndexError: index 2 is out of bounds for axis 0 with size 2 -> Option D
Quick Check:
axes shape (2,2), index 2 invalid [OK]
Hint: Check axes array shape before indexing [OK]
Common Mistakes:
Using invalid indices beyond shape
Confusing axes as list instead of array
Expecting no error on wrong index
5. You want to create a 2x3 grid of subplots and plot a line on each subplot with data from a list of 6 arrays. Which code correctly accesses each subplot to plot inside a nested loop?
hard
A. for i in range(2):
for j in range(3):
axes[i][j].plot(data[i*3 + j])
B. for i in range(2):
for j in range(3):
axes[i, j].plot(data[i + j])
C. for i in range(3):
for j in range(2):
axes[i][j].plot(data[i*2 + j])
D. for i in range(2):
for j in range(3):
axes[j][i].plot(data[i*3 + j])
Solution
Step 1: Understand axes shape and indexing
plt.subplots(2, 3) creates axes as a 2x3 array, indexed as axes[row][col].
Step 2: Correctly map data list to subplot indices
Data index = row * number_of_columns + column = i*3 + j matches subplot position.
Final Answer:
for i in range(2):
for j in range(3):
axes[i][j].plot(data[i*3 + j]) -> Option A
Quick Check:
axes[row][col], data index = row*cols + col [OK]
Hint: Use axes[row][col] and data index = row*cols + col [OK]
Common Mistakes:
Swapping row and column loops
Incorrect data indexing
Using axes[j][i] instead of axes[i][j]
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.