Challenge - 5 Problems
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What does this code output?
Consider the following code using
plt.subplots(). What will be the type of ax?Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() print(type(ax))
Attempts:
2 left
💡 Hint
Remember that
plt.subplots() returns a figure and axes object.✗ Incorrect
The
plt.subplots() function returns a tuple: the first element is a Figure object, and the second is an AxesSubplot object representing the plot area.❓ data_output
intermediate1:30remaining
How many axes are created?
What is the shape and type of
ax after running this code?Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(2, 3) print(type(ax)) print(ax.shape)
Attempts:
2 left
💡 Hint
When multiple subplots are created,
ax is an array of axes.✗ Incorrect
With
plt.subplots(2, 3), ax is a 2x3 numpy array of AxesSubplot objects.❓ visualization
advanced2:00remaining
Identify the correct subplot layout
Which option shows the correct subplot layout for
fig, ax = plt.subplots(1, 4)?Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 4) for i, axis in enumerate(ax): axis.plot([0, 1], [i, i+1]) plt.show()
Attempts:
2 left
💡 Hint
The arguments (1, 4) mean 1 row and 4 columns.
✗ Incorrect
The
plt.subplots(1, 4) creates one row with four columns of subplots arranged horizontally.🔧 Debug
advanced1:30remaining
Why does this code raise an error?
What error does this code raise and why?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax[0].plot([1, 2, 3])
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax[0].plot([1, 2, 3])
Attempts:
2 left
💡 Hint
Check the type of
ax when only one subplot is created.✗ Incorrect
When only one subplot is created,
ax is a single AxesSubplot object, not a list or array, so indexing it causes a TypeError.🚀 Application
expert2:00remaining
How to flatten axes array for iteration?
Given
fig, ax = plt.subplots(3, 2), which option correctly iterates over all axes to plot a line on each?Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(3, 2) # Fill in the blank to plot on each axis for axis in _____: axis.plot([0, 1], [1, 2]) plt.show()
Attempts:
2 left
💡 Hint
The axes array is 2D; flatten it to 1D for simple iteration.
✗ Incorrect
The
ax is a 2D numpy array of axes. Using ax.flatten() converts it to a 1D array for easy iteration.