0
0
Matplotlibdata~20 mins

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

Choose your learning style9 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.