Challenge - 5 Problems
Axes Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the output shape of the figure's axes array?
Consider the following code that creates a figure with multiple subplots using
add_subplot. What is the shape of the axes array after running this code?Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() axes = [fig.add_subplot(2, 2, i) for i in range(1, 5)] print(len(axes))
Attempts:
2 left
💡 Hint
Each call to add_subplot adds one subplot to the figure.
✗ Incorrect
The code creates 4 subplots arranged in a 2x2 grid. The list 'axes' contains one Axes object per subplot, so its length is 4.
❓ data_output
intermediate1:30remaining
What is the type of the object returned by add_subplot?
After running the code below, what is the type of the variable
ax?Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) print(type(ax))
Attempts:
2 left
💡 Hint
add_subplot returns an Axes object representing a subplot.
✗ Incorrect
The add_subplot method returns an AxesSubplot object which is a type of Axes used for plotting.
❓ visualization
advanced2:00remaining
Identify the subplot layout from the code
Given the code below, what is the layout of the subplots created in the figure?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(2, 3, 1) ax2 = fig.add_subplot(2, 3, 4) ax3 = fig.add_subplot(2, 3, 6) plt.show()
Attempts:
2 left
💡 Hint
The first two arguments to add_subplot define rows and columns.
✗ Incorrect
The code creates a 2x3 grid of subplots. The third argument specifies the position in row-major order. Positions 1, 4, and 6 correspond to the first subplot in the first row, first subplot in the second row, and last subplot in the second row respectively.
🔧 Debug
advanced1:30remaining
Why does this add_subplot call raise an error?
Examine the code below. Why does the call to
add_subplot(3, 2, 7) raise an error?Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(3, 2, 7)
Attempts:
2 left
💡 Hint
The third argument must be between 1 and rows*columns inclusive.
✗ Incorrect
For a 3 rows by 2 columns grid, the maximum position index is 6. Using 7 is out of range and causes an error.
🚀 Application
expert2:00remaining
How to create a figure with 3 subplots stacked vertically using add_subplot?
You want to create a figure with 3 subplots stacked vertically (one column, three rows). Which code snippet correctly creates these subplots and stores them in a list
axes?Attempts:
2 left
💡 Hint
Rows come first, then columns, then position.
✗ Incorrect
To stack subplots vertically, use 3 rows and 1 column. Positions 1 to 3 create the three subplots stacked vertically.