0
0
Matplotlibdata~20 mins

Axes creation with add_subplot in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axes Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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))
A2
B4
C1
D0
Attempts:
2 left
💡 Hint
Each call to add_subplot adds one subplot to the figure.
data_output
intermediate
1: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))
A<class 'matplotlib.axes._subplots.AxesSubplot'>
B<class 'matplotlib.figure.Figure'>
C<class 'list'>
D<class 'tuple'>
Attempts:
2 left
💡 Hint
add_subplot returns an Axes object representing a subplot.
visualization
advanced
2: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()
A3 rows and 2 columns with subplots at positions 2, 4, and 6
B3 rows and 2 columns with subplots at positions 1, 4, and 6
C2 rows and 3 columns with subplots at positions 1, 2, and 3
D2 rows and 3 columns with subplots at positions 1, 4, and 6
Attempts:
2 left
💡 Hint
The first two arguments to add_subplot define rows and columns.
🔧 Debug
advanced
1: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)
AThe position index 7 is invalid for a 3x2 grid (max is 6)
BThe number of columns cannot be 2 in add_subplot
CThe number of rows cannot be 3 in add_subplot
Dadd_subplot requires only two arguments, not three
Attempts:
2 left
💡 Hint
The third argument must be between 1 and rows*columns inclusive.
🚀 Application
expert
2: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?
Aaxes = [fig.add_subplot(3, 3, i) for i in range(1, 4)]
Baxes = [fig.add_subplot(1, 3, i) for i in range(1, 4)]
Caxes = [fig.add_subplot(3, 1, i) for i in range(1, 4)]
Daxes = [fig.add_subplot(1, 1, i) for i in range(1, 4)]
Attempts:
2 left
💡 Hint
Rows come first, then columns, then position.