Challenge - 5 Problems
Subplot Grid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Check how subplots are arranged and how images are assigned to axes.
✗ Incorrect
The code creates a 1x2 grid of subplots. The first subplot shows a white 5x5 image (all ones), the second shows a black 5x5 image (all zeros).
❓ data_output
intermediate1: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()))
Attempts:
2 left
💡 Hint
Count rows times columns in the subplot grid.
✗ Incorrect
A 3x3 grid creates 9 axes objects. Flattening axs gives a list of all 9 axes.
❓ visualization
advanced2: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?
Attempts:
2 left
💡 Hint
Check the shape of the subplot grid and how images are assigned.
✗ Incorrect
Option B creates a 2x2 grid and fills each subplot with a different image. Others create wrong grid shapes or use extra subplots.
🔧 Debug
advanced1: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()
Attempts:
2 left
💡 Hint
Check the type of axs when subplots(1,1) is used.
✗ Incorrect
When subplots(1,1) is called, axs is a single Axes object, not an array. It has imshow method, so no error occurs.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Check the shape of the grid and the sharex/sharey parameters.
✗ Incorrect
Option A creates a 2x3 grid with shared x and y axes, matching the requirement. Option A swaps rows and columns, A lacks shared axes, C creates a 6x1 grid.