Challenge - 5 Problems
Figure and Axes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the output of this figure and axes creation code?
Consider the following Python code using matplotlib to create a figure and axes. What will be the type of the variable
ax?Data Analysis Python
import matplotlib.pyplot as plt fig, ax = plt.subplots() print(type(ax))
Attempts:
2 left
💡 Hint
The
plt.subplots() function returns a figure and axes object. The axes object is used to plot data.✗ Incorrect
The
plt.subplots() function returns a tuple: the first element is a Figure object, and the second is an AxesSubplot object. The variable ax is the axes, so its type is matplotlib.axes._subplots.AxesSubplot.❓ data_output
intermediate1:30remaining
How many axes are created in this figure?
What is the number of axes created by the following code?
Data Analysis Python
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 3) print(len(axs.flatten()))
Attempts:
2 left
💡 Hint
The
plt.subplots(2, 3) creates a grid of 2 rows and 3 columns of axes.✗ Incorrect
The
plt.subplots(2, 3) creates 2 rows and 3 columns of axes, so total axes are 2 * 3 = 6. The axs is a 2D array of axes, and flattening it gives a list of all 6 axes.🔧 Debug
advanced2:00remaining
What error does this code raise?
Examine the code below. What error will it raise when run?
Data Analysis Python
import matplotlib.pyplot as plt fig, ax = plt.subplots(2) ax.plot([1, 2, 3], [4, 5, 6])
Attempts:
2 left
💡 Hint
When
plt.subplots(2) is called, ax is an array of axes, not a single axes.✗ Incorrect
Calling
plt.subplots(2) creates a figure with 2 axes in a 1D array. The variable ax is an array, which does not have a plot method. Trying to call ax.plot() raises an AttributeError.❓ visualization
advanced1:30remaining
Which option produces a figure with 4 subplots arranged in 2 rows and 2 columns?
Select the code snippet that creates a figure with 4 subplots arranged in 2 rows and 2 columns.
Attempts:
2 left
💡 Hint
The first argument is number of rows, the second is number of columns.
✗ Incorrect
The
plt.subplots(2, 2) creates 2 rows and 2 columns of subplots, totaling 4 subplots.🧠 Conceptual
expert2:30remaining
What is the difference between
fig.add_subplot() and plt.subplots()?Choose the correct statement about the difference between
fig.add_subplot() and plt.subplots() in matplotlib.Attempts:
2 left
💡 Hint
Think about whether a new figure is created or an existing figure is modified.
✗ Incorrect
fig.add_subplot() is a method to add a single subplot to an existing figure object. plt.subplots() is a function that creates a new figure and a grid of subplots (axes). They serve different purposes.