Bird
Raised Fist0
Matplotlibdata~20 mins

Plt.subplots with rows and columns in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the shape of the axes array?
Consider this code snippet using plt.subplots with 3 rows and 2 columns. What is the shape of the axes object?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 2)
print(axes.shape)
A(2, 3)
B()
C(6,)
D(3, 2)
Attempts:
2 left
💡 Hint
The axes is a 2D array matching rows and columns.
❓ data_output
intermediate
1:00remaining
How many axes objects are created?
What is the total number of axes objects created by plt.subplots(4, 1)?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4, 1)
print(len(axes))
A4
B1
C5
D0
Attempts:
2 left
💡 Hint
Number of axes equals rows times columns.
❓ visualization
advanced
2:00remaining
Identify the subplot layout from the plot
You run this code to create subplots and plot data. Which layout matches the figure shown?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 3)
for i, ax in enumerate(axes.flat):
    ax.plot(x, np.sin(x + i))
plt.show()
A2 rows and 3 columns
B3 rows and 2 columns
C1 row and 6 columns
D6 rows and 1 column
Attempts:
2 left
💡 Hint
Check how many rows and columns are passed to plt.subplots.
🔧 Debug
advanced
1:30remaining
Why does this code raise an error?
This code tries to plot on subplots but raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
axes[2].plot([1, 2, 3])
plt.show()
AValueError because plot data is invalid
BTypeError because axes is not subscriptable
CIndexError because axes is a 2D array and index 2 is invalid
DNo error, code runs fine
Attempts:
2 left
💡 Hint
Check the shape of axes and how indexing works.
🚀 Application
expert
2:00remaining
How to flatten axes for iteration?
You create subplots with 3 rows and 3 columns. You want to loop over all axes in one loop. Which code correctly flattens the axes array?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 3)
# Which line below correctly flattens axes for iteration?
A
for ax in axes.flat:
    ax.plot([1, 2, 3])
B
for ax in axes.flatten():
    ax.plot([1, 2, 3])
C
for ax in axes.ravel():
    ax.plot([1, 2, 3])
D
for ax in axes:
    ax.plot([1, 2, 3])
Attempts:
2 left
💡 Hint
Check the methods available on numpy arrays for flattening.

Practice

(1/5)
1. What does plt.subplots(2, 3) create in matplotlib?
easy
A. A figure with 2 rows and 3 columns of subplots
B. A figure with 3 rows and 2 columns of subplots
C. A single plot with size 2x3 inches
D. A figure with 6 rows and 1 column of subplots

Solution

  1. Step 1: Understand plt.subplots parameters

    The first parameter is the number of rows, the second is columns.
  2. Step 2: Apply to given code

    plt.subplots(2, 3) means 2 rows and 3 columns of subplots.
  3. Final Answer:

    A figure with 2 rows and 3 columns of subplots -> Option A
  4. Quick Check:

    rows=2, columns=3 = B [OK]
Hint: First number = rows, second = columns in plt.subplots [OK]
Common Mistakes:
  • Confusing rows and columns order
  • Thinking parameters set figure size
  • Assuming single plot created
2. Which of the following is the correct syntax to create a 3x1 subplot grid using plt.subplots?
easy
A. fig, axes = plt.subplots(1, 3)
B. fig, axes = plt.subplots(3)
C. fig, axes = plt.subplots(3, 1)
D. fig, axes = plt.subplots(rows=3, cols=1)

Solution

  1. Step 1: Recall plt.subplots parameter order

    The first argument is rows, second is columns.
  2. Step 2: Match to 3 rows and 1 column

    plt.subplots(3, 1) creates 3 rows and 1 column.
  3. Final Answer:

    fig, axes = plt.subplots(3, 1) -> Option C
  4. Quick Check:

    3 rows, 1 column = C [OK]
Hint: Remember: plt.subplots(rows, columns) order matters [OK]
Common Mistakes:
  • Swapping rows and columns
  • Using single number for both dimensions
  • Using keyword arguments incorrectly
3. What is the shape of axes returned by fig, axes = plt.subplots(2, 2)?
medium
A. A 1D array with 4 axes objects
B. A 2D array with shape (2, 2) of axes objects
C. A list of 2 axes objects
D. A single axes object

Solution

  1. Step 1: Understand axes shape for multiple rows and columns

    When rows and columns are more than 1, axes is a 2D array matching shape.
  2. Step 2: Apply to plt.subplots(2, 2)

    axes will be a 2x2 array of axes objects.
  3. Final Answer:

    A 2D array with shape (2, 2) of axes objects -> Option B
  4. Quick Check:

    Multiple rows and columns = 2D axes array [OK]
Hint: Multiple rows & columns -> axes is 2D array [OK]
Common Mistakes:
  • Assuming axes is always 1D
  • Confusing number of axes with shape
  • Expecting a single axes object
4. Given the code:
fig, axes = plt.subplots(2, 2)
axes[2, 0].plot([1, 2, 3])

What error will occur?
medium
A. No error, plot will display
B. TypeError: axes object is not subscriptable
C. AttributeError: 'AxesSubplot' object has no attribute 'plot'
D. IndexError: index 2 is out of bounds for axis 0 with size 2

Solution

  1. Step 1: Check axes shape from plt.subplots(2, 2)

    axes is a 2x2 array with indices 0 and 1 for rows and columns.
  2. Step 2: Analyze axes[2, 0] access

    Index 2 for rows is out of bounds since max index is 1.
  3. Final Answer:

    IndexError: index 2 is out of bounds for axis 0 with size 2 -> Option D
  4. 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

  1. Step 1: Understand axes shape and indexing

    plt.subplots(2, 3) creates axes as a 2x3 array, indexed as axes[row][col].
  2. Step 2: Correctly map data list to subplot indices

    Data index = row * number_of_columns + column = i*3 + j matches subplot position.
  3. Final Answer:

    for i in range(2): for j in range(3): axes[i][j].plot(data[i*3 + j]) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard