Bird
Raised Fist0
Matplotlibdata~20 mins

Nested subplots with subfigures in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Subplots Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested subplots with subfigures

What will be the output of the following code that creates nested subplots using subfigures in matplotlib?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig = plt.figure(constrained_layout=True)
subfigs = fig.subfigures(1, 2)

ax1 = subfigs[0].subplots(2, 1)
ax1[0].plot(x, np.sin(x))
ax1[1].plot(x, np.cos(x))

ax2 = subfigs[1].subplots(1, 2)
ax2[0].plot(x, np.tan(x))
ax2[1].plot(x, np.exp(-x))

plt.show()
AA figure with two subfigures stacked vertically; each subfigure contains two plots side by side.
BA figure with two subfigures side by side; left subfigure has two rows of plots (sin and cos), right subfigure has two columns of plots (tan and exp decay).
CA single figure with four plots arranged in a 2x2 grid showing sin, cos, tan, and exp decay.
DAn error occurs because subfigures cannot be nested inside a figure.
Attempts:
2 left
💡 Hint

Think about how subfigures split the main figure and how subplots inside each subfigure arrange axes.

❓ data_output
intermediate
1:30remaining
Number of axes in nested subplots with subfigures

How many total axes (individual plots) are created by the following code?

Matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
subfigs = fig.subfigures(2, 1)

ax_top = subfigs[0].subplots(1, 3)
ax_bottom = subfigs[1].subplots(2, 2)

print(len(ax_top) + ax_bottom.size)
A7
B6
C8
D5
Attempts:
2 left
💡 Hint

Count the number of axes in each subfigure and add them.

🔧 Debug
advanced
1:30remaining
Identify the error in nested subfigures code

What error will this code raise?

Matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
subfigs = fig.subfigures(1, 2)

ax = subfigs.subplots(2, 2)

plt.show()
AAttributeError: 'SubFigure' object has no attribute 'subplots'
BAttributeError: 'numpy.ndarray' object has no attribute 'subfigures'
CTypeError: subfigures() missing 1 required positional argument
DAttributeError: 'numpy.ndarray' object has no attribute 'subplots'
Attempts:
2 left
💡 Hint

Check the type of subfigs and what methods it supports.

❓ visualization
advanced
2:00remaining
Visual layout of nested subplots with subfigures

Which option best describes the layout of the figure created by this code?

Matplotlib
import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True)
subfigs = fig.subfigures(2, 1)

ax1 = subfigs[0].subplots(1, 2)
ax2 = subfigs[1].subplots(3, 1)

plt.show()
AOne figure with five plots arranged in a 3x2 grid.
BTwo subfigures side by side; left subfigure has two plots stacked vertically, right subfigure has three plots side by side.
CTwo subfigures stacked vertically; top subfigure has two plots side by side, bottom subfigure has three plots stacked vertically.
DA single subplot with five lines plotted.
Attempts:
2 left
💡 Hint

Look at the subfigures(2, 1) call and how subplots are arranged inside each subfigure.

🚀 Application
expert
3:00remaining
Creating a complex nested subplot layout with subfigures

You want to create a figure with three subfigures arranged horizontally. The first subfigure has a 2x1 grid of plots, the second subfigure has a single plot, and the third subfigure has a 2x2 grid of plots. Which code snippet correctly creates this layout?

A
fig = plt.figure()
subfigs = fig.subfigures(1, 3)
ax1 = subfigs[0].subplots(2, 1)
ax2 = subfigs[1].subplots(1, 1)
ax3 = subfigs[2].subplots(2, 2)
B
fig = plt.figure()
subfigs = fig.subfigures(3, 1)
ax1 = subfigs[0].subplots(1, 2)
ax2 = subfigs[1].subplots(1, 1)
ax3 = subfigs[2].subplots(2, 2)
C
fig = plt.figure()
subfigs = fig.subfigures(1, 3)
ax1 = subfigs[0].subplots(1, 2)
ax2 = subfigs[1].subplots(1, 1)
ax3 = subfigs[2].subplots(2, 2)
D
fig = plt.figure()
subfigs = fig.subfigures(1, 3)
ax1 = subfigs[0].subplots(2, 1)
ax2 = subfigs[1].subplots(2, 2)
ax3 = subfigs[2].subplots(1, 1)
Attempts:
2 left
💡 Hint

Remember the first argument to subfigures is rows, second is columns. Match the grids inside each subfigure carefully.

Practice

(1/5)
1. What is the main purpose of using fig.subfigures() in matplotlib?
easy
A. To add a single plot to a figure
B. To create large groups inside a figure that can hold their own smaller plots
C. To change the color of a plot
D. To save a figure as an image file

Solution

  1. Step 1: Understand the role of fig.subfigures()

    This method creates subfigures, which are large groups inside a main figure.
  2. Step 2: Recognize the use of subfigures

    Each subfigure can contain its own grid of smaller plots, helping organize complex layouts.
  3. Final Answer:

    To create large groups inside a figure that can hold their own smaller plots -> Option B
  4. Quick Check:

    Subfigures = large groups inside figure [OK]
Hint: Subfigures group plots inside a figure [OK]
Common Mistakes:
  • Thinking subfigures add only one plot
  • Confusing subfigures with saving figures
  • Assuming subfigures change plot colors
2. Which of the following is the correct way to create two subfigures side by side in matplotlib?
easy
A. fig.subplots(1, 2)
B. fig.subfigures(2, 1)
C. fig.subfigures(1, 2)
D. fig.subfigures(2, 2)

Solution

  1. Step 1: Understand subfigures layout parameters

    The parameters (rows, columns) define the grid of subfigures. For side by side, use 1 row and 2 columns.
  2. Step 2: Match parameters to options

    fig.subfigures(1, 2) uses fig.subfigures(1, 2), which creates 1 row and 2 columns, placing subfigures side by side.
  3. Final Answer:

    fig.subfigures(1, 2) -> Option C
  4. Quick Check:

    Side by side = 1 row, 2 columns [OK]
Hint: Rows first, columns second in subfigures() [OK]
Common Mistakes:
  • Swapping rows and columns
  • Using subplots() instead of subfigures()
  • Choosing more rows than needed
3. What will be the output shape of the axes array when running this code?
fig = plt.figure()
subfigs = fig.subfigures(2, 1)
ax1 = subfigs[0].subplots(1, 3)
ax2 = subfigs[1].subplots(2, 2)
print(ax1.shape, ax2.shape)
medium
A. (1, 3) (4,)
B. (3,) (4,)
C. (3,) (2, 2)
D. (1, 3) (2, 2)

Solution

  1. Step 1: Analyze subfigures and their subplot grids

    First subfigure has 1 row and 3 columns, so ax1 shape is (1, 3). Second subfigure has 2 rows and 2 columns, so ax2 shape is (2, 2).
  2. Step 2: Confirm shapes printed

    Printing ax1.shape and ax2.shape outputs (1, 3) and (2, 2) respectively.
  3. Final Answer:

    (1, 3) (2, 2) -> Option D
  4. Quick Check:

    Subplots shape = (rows, columns) [OK]
Hint: Shape matches (rows, columns) in subplots() [OK]
Common Mistakes:
  • Confusing flat arrays with 2D arrays
  • Mixing up rows and columns
  • Assuming subfigures affect shape differently
4. Identify the error in this code snippet for creating nested subplots with subfigures:
fig = plt.figure()
subfigs = fig.subfigures(1, 2)
ax = subfigs.subplots(2, 2)
medium
A. subfigs.subplots() should be called on each subfigure, not on the list
B. fig.subfigures() requires 3 arguments, missing one
C. subfigs is a list, so subfigs.subplots() is invalid
D. subfigures cannot have nested subplots

Solution

  1. Step 1: Understand the type of subfigs

    fig.subfigures(1, 2) returns a list or array of subfigure objects, not a single subfigure.
  2. Step 2: Correct method call for subplots

    We must call subplots() on each individual subfigure, not on the entire list subfigs.
  3. Final Answer:

    subfigs.subplots() should be called on each subfigure, not on the list -> Option A
  4. Quick Check:

    Call subplots() on single subfigure [OK]
Hint: Call subplots() on one subfigure, not list [OK]
Common Mistakes:
  • Calling subplots() on the list of subfigures
  • Assuming subfigures() needs 3 args
  • Thinking subfigures can't have subplots
5. You want to create a figure with 2 subfigures stacked vertically. The top subfigure should have a 2x2 grid of plots, and the bottom subfigure a single plot. Which code correctly achieves this?
hard
A. fig = plt.figure() subfigs = fig.subfigures(2, 1) axs_top = subfigs[0].subplots(2, 2) axs_bottom = subfigs[1].subplots(1, 1)
B. fig = plt.figure() subfigs = fig.subfigures(1, 2) axs_top = subfigs[0].subplots(2, 2) axs_bottom = subfigs[1].subplots(1, 1)
C. fig = plt.figure() subfigs = fig.subfigures(2, 1) axs_top = subfigs.subplots(2, 2) axs_bottom = subfigs.subplots(1, 1)
D. fig = plt.figure() subfigs = fig.subfigures(1, 2) axs_top = subfigs.subplots(2, 2) axs_bottom = subfigs.subplots(1, 1)

Solution

  1. Step 1: Set subfigures layout vertically

    Two subfigures stacked vertically means 2 rows and 1 column, so fig.subfigures(2, 1).
  2. Step 2: Create subplots inside each subfigure

    Top subfigure gets a 2x2 grid, bottom subfigure gets a single plot (1x1). Call subplots() on each subfigure individually.
  3. Final Answer:

    fig = plt.figure() subfigs = fig.subfigures(2, 1) axs_top = subfigs[0].subplots(2, 2) axs_bottom = subfigs[1].subplots(1, 1) -> Option A
  4. Quick Check:

    Vertical stack = (2,1), call subplots() on each subfigure [OK]
Hint: Vertical stack = (2,1), subplots() on each subfigure [OK]
Common Mistakes:
  • Using (1,2) for vertical stacking
  • Calling subplots() on the list of subfigures
  • Not matching subplot grids to subfigures
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