0
0
Matplotlibdata~20 mins

GridSpec for complex layouts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GridSpec Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this GridSpec subplot arrangement?

Consider the following code using matplotlib.gridspec.GridSpec. What will be the number of subplots created and their arrangement?

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(3, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :-1])
ax3 = fig.add_subplot(gs[1:, -1])
ax4 = fig.add_subplot(gs[-1, 0])
plt.close(fig)
print(len(fig.axes))
A5
B4
C3
D6
Attempts:
2 left
💡 Hint

Count each add_subplot call and check if any overlap or replacement occurs.

data_output
intermediate
2:00remaining
What is the shape of the GridSpec layout?

Given this GridSpec code, what is the shape of the grid and which cells does gs[1:3, 0:2] cover?

Matplotlib
from matplotlib.gridspec import GridSpec

gs = GridSpec(4, 4)
covered_cells = [(r, c) for r in range(1, 3) for c in range(0, 2)]
print((gs.nrows, gs.ncols), covered_cells)
A(4, 4) and cells [(1, 0), (1, 1), (2, 0), (2, 1)]
B(3, 3) and cells [(1, 0), (1, 1), (2, 0), (2, 1)]
C(4, 4) and cells [(0, 1), (0, 2), (1, 1), (1, 2)]
D(3, 3) and cells [(0, 1), (0, 2), (1, 1), (1, 2)]
Attempts:
2 left
💡 Hint

GridSpec(4,4) creates a 4x4 grid. The slice selects rows 1 and 2, columns 0 and 1.

visualization
advanced
2:30remaining
Identify the correct GridSpec layout visualization

Which option shows the correct layout of subplots created by this code?

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure(figsize=(6, 4))
gs = GridSpec(2, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1:])
ax3 = fig.add_subplot(gs[1, :2])
ax4 = fig.add_subplot(gs[1, 2])
plt.close(fig)
# The question is about the layout shape, not the plot content.
AOne small subplot on top left, one spanning top right two columns, one spanning bottom left two columns, one small bottom right
BFour equally sized subplots in a 2x2 grid
CThree subplots stacked vertically
DTwo subplots side by side on top, two stacked vertically on bottom left
Attempts:
2 left
💡 Hint

Look at the slices: gs[0, 0], gs[0, 1:], gs[1, :2], gs[1, 2].

🔧 Debug
advanced
1:30remaining
What error does this GridSpec code raise?

What error will this code raise when executed?

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(2, 2, figure=fig)
ax1 = fig.add_subplot(gs[2, 0])
plt.close(fig)
ATypeError: 'GridSpec' object is not subscriptable
BValueError: Invalid subplot specification
CIndexError: index 2 is out of bounds for axis 0 with size 2
DNo error, subplot created successfully
Attempts:
2 left
💡 Hint

Check the grid size and the index used in gs[2, 0].

🚀 Application
expert
3:00remaining
How to create a complex layout with nested GridSpec?

You want to create a figure with two main columns. The left column has two rows of equal height. The right column is a single subplot spanning full height. Which code snippet correctly creates this layout using nested GridSpec?

A
fig = plt.figure()
gs = GridSpec(2, 2, figure=fig)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[:, 1])
B
fig = plt.figure()
gs = GridSpec(2, 1, figure=fig)
gs_left = GridSpec(1, 2, subplot_spec=gs[0, 0])
ax1 = fig.add_subplot(gs_left[0, 0])
ax2 = fig.add_subplot(gs_left[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
C
fig = plt.figure()
gs = GridSpec(1, 2, figure=fig)
gs_left = GridSpec(1, 2, subplot_spec=gs[0, 0])
ax1 = fig.add_subplot(gs_left[0, 0])
ax2 = fig.add_subplot(gs_left[0, 1])
ax3 = fig.add_subplot(gs[0, 1])
D
fig = plt.figure()
gs = GridSpec(1, 2, figure=fig)
gs_left = GridSpec(2, 1, subplot_spec=gs[0, 0])
ax1 = fig.add_subplot(gs_left[0, 0])
ax2 = fig.add_subplot(gs_left[1, 0])
ax3 = fig.add_subplot(gs[0, 1])
Attempts:
2 left
💡 Hint

Think about how to split the figure into two columns, then split the left column into two rows.