Challenge - 5 Problems
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a 2x2 subplot grid with line plots
What is the output of this code that creates a 2x2 grid of line plots with matplotlib?
Data Analysis Python
import matplotlib.pyplot as plt import numpy as np x = np.arange(5) fig, axs = plt.subplots(2, 2) for i, ax in enumerate(axs.flat): ax.plot(x, x*(i+1)) plt.tight_layout() plt.show()
Attempts:
2 left
💡 Hint
Remember that axs.flat lets you iterate over all subplot axes in a grid.
✗ Incorrect
The code creates a 2x2 grid of subplots. The loop uses axs.flat to access each subplot axis and plots a line with slope increasing from 1 to 4. The result is four separate line plots arranged in a 2x2 grid.
❓ data_output
intermediate1:00remaining
Number of subplots created by plt.subplots(3,1)
How many subplot axes are created by the command plt.subplots(3, 1)?
Attempts:
2 left
💡 Hint
The first argument is rows, the second is columns.
✗ Incorrect
plt.subplots(3, 1) creates 3 subplot axes arranged vertically in a single column.
❓ visualization
advanced2:00remaining
Identify the subplot layout from the code
Given this code, what is the layout of the subplots shown?
Data Analysis Python
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 3, figsize=(9, 3)) for i, ax in enumerate(axs): ax.bar([1, 2, 3], [i+1, i+2, i+3]) plt.show()
Attempts:
2 left
💡 Hint
plt.subplots(1, 3) means 1 row and 3 columns.
✗ Incorrect
The code creates a figure with 1 row and 3 columns of subplots. Each subplot shows a bar chart. So the layout is three bar charts side by side.
🔧 Debug
advanced1:30remaining
Why does this subplot code raise an error?
What error does this code raise and why?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[2].plot([1, 2, 3], [4, 5, 6])
plt.show()
Attempts:
2 left
💡 Hint
axs is a 2D array of axes, not a flat list.
✗ Incorrect
axs is a 2x2 numpy array of axes. Indexing axs[2] tries to access the third row which does not exist, causing IndexError.
🚀 Application
expert3:00remaining
Create a 2x3 grid of scatter plots with different colors
You want to create a 2 rows by 3 columns grid of scatter plots using matplotlib. Each subplot should plot points (x, y) where x = [1,2,3] and y = [3,2,1] multiplied by the subplot index (starting at 1). Each subplot should have a unique color from ['red', 'green', 'blue', 'orange', 'purple', 'brown']. Which code snippet correctly creates this figure?
Attempts:
2 left
💡 Hint
Use axs.flat to iterate over all axes in a 2D grid and start index at 1 for multiplication.
✗ Incorrect
Option C correctly creates a 2x3 grid, iterates over all 6 axes with index starting at 1, multiplies y by i, and assigns colors correctly. Other options have wrong shape, indexing, or iteration errors.