0
0
Matplotlibdata~20 mins

Multiple images in subplot grid in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subplot Grid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of subplot grid with 2 images
What will be the output of this code that plots two images side by side using matplotlib subplots?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img1 = np.ones((5,5))
img2 = np.zeros((5,5))

fig, axs = plt.subplots(1, 2)
axs[0].imshow(img1, cmap='gray')
axs[1].imshow(img2, cmap='gray')
plt.show()
AA figure with two images: left is a white square, right is a black square.
BAn error because axs is not iterable.
CA single image showing a gradient from black to white.
DA figure with two images stacked vertically.
Attempts:
2 left
💡 Hint
Check how subplots are arranged and how images are assigned to axes.
data_output
intermediate
1:00remaining
Number of axes in a 3x3 subplot grid
How many axes objects are created when you run this code?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 3)
print(len(axs.flatten()))
A1
B3
C9
D6
Attempts:
2 left
💡 Hint
Count rows times columns in the subplot grid.
visualization
advanced
2:30remaining
Identify the subplot layout from the image
Given a figure with 4 images arranged in a 2x2 grid, which code snippet correctly creates this layout?
A
fig, axs = plt.subplots(1, 4)
for i, ax in enumerate(axs):
    ax.imshow(np.full((3,3), i))
B
fig, axs = plt.subplots(2, 2)
for i, ax in enumerate(axs.flatten()):
    ax.imshow(np.full((3,3), i))
C
fig, axs = plt.subplots(4, 1)
for i, ax in enumerate(axs):
    ax.imshow(np.full((3,3), i))
D
fig, axs = plt.subplots(3, 3)
for i, ax in enumerate(axs.flatten()[:4]):
    ax.imshow(np.full((3,3), i))
Attempts:
2 left
💡 Hint
Check the shape of the subplot grid and how images are assigned.
🔧 Debug
advanced
1:30remaining
Why does this subplot code raise an error?
This code raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 1)
axs.imshow([[1, 2], [3, 4]])
plt.show()
A'AxesSubplot' object has no attribute 'imshow'
BTypeError because axs is not an array but a single Axes object
CIndexError because axs is not subscriptable
DNo error, the code runs fine
Attempts:
2 left
💡 Hint
Check the type of axs when subplots(1,1) is used.
🚀 Application
expert
3:00remaining
Arrange 6 images in a 2x3 grid with shared axes
You want to plot 6 images in a 2 rows by 3 columns grid using matplotlib. You want all subplots to share the same x and y axes to compare images easily. Which code snippet achieves this?
A
fig, axs = plt.subplots(2, 3, sharex=True, sharey=True)
for ax in axs.flatten():
    ax.imshow(np.random.rand(5,5))
plt.show()
B
fig, axs = plt.subplots(2, 3)
for ax in axs.flatten():
    ax.imshow(np.random.rand(5,5))
plt.show()
C
fig, axs = plt.subplots(6, 1, sharex=True, sharey=True)
for ax in axs:
    ax.imshow(np.random.rand(5,5))
plt.show()
D
fig, axs = plt.subplots(3, 2, sharex=True, sharey=True)
for ax in axs.flatten():
    ax.imshow(np.random.rand(5,5))
plt.show()
Attempts:
2 left
💡 Hint
Check the shape of the grid and the sharex/sharey parameters.