Challenge - 5 Problems
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the shape of the axes array?
Consider this code snippet using
plt.subplots with 3 rows and 2 columns. What is the shape of the axes object?Matplotlib
import matplotlib.pyplot as plt fig, axes = plt.subplots(3, 2) print(axes.shape)
Attempts:
2 left
💡 Hint
The
axes is a 2D array matching rows and columns.✗ Incorrect
When you create subplots with 3 rows and 2 columns,
axes is a 2D numpy array with shape (3, 2).❓ data_output
intermediate1:00remaining
How many axes objects are created?
What is the total number of axes objects created by
plt.subplots(4, 1)?Matplotlib
import matplotlib.pyplot as plt fig, axes = plt.subplots(4, 1) print(len(axes))
Attempts:
2 left
💡 Hint
Number of axes equals rows times columns.
✗ Incorrect
With 4 rows and 1 column,
axes is a 1D array of length 4.❓ visualization
advanced2:00remaining
Identify the subplot layout from the plot
You run this code to create subplots and plot data. Which layout matches the figure shown?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) fig, axes = plt.subplots(2, 3) for i, ax in enumerate(axes.flat): ax.plot(x, np.sin(x + i)) plt.show()
Attempts:
2 left
💡 Hint
Check how many rows and columns are passed to
plt.subplots.✗ Incorrect
The code creates 2 rows and 3 columns of subplots, so the figure shows 6 plots arranged in 2 rows and 3 columns.
🔧 Debug
advanced1:30remaining
Why does this code raise an error?
This code tries to plot on subplots but raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2) axes[2].plot([1, 2, 3]) plt.show()
Attempts:
2 left
💡 Hint
Check the shape of axes and how indexing works.
✗ Incorrect
The
axes is a 2x2 array, so axes[2] is out of bounds. You must use two indices like axes[0, 1].🚀 Application
expert2:00remaining
How to flatten axes for iteration?
You create subplots with 3 rows and 3 columns. You want to loop over all axes in one loop. Which code correctly flattens the axes array?
Matplotlib
import matplotlib.pyplot as plt fig, axes = plt.subplots(3, 3) # Which line below correctly flattens axes for iteration?
Attempts:
2 left
💡 Hint
Check the methods available on numpy arrays for flattening.
✗ Incorrect
The
flatten() method returns a 1D copy of the array, allowing iteration over all axes.