0
0
Matplotlibdata~20 mins

Figure and Axes mental model in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Figure and Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code?
Consider the following code using matplotlib. What will be the type of the variable ax?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
print(type(ax))
A<class 'matplotlib.axes._subplots.AxesSubplot'>
B<class 'matplotlib.figure.Figure'>
C<class 'list'>
D<class 'tuple'>
Attempts:
2 left
💡 Hint
Remember that plt.subplots() returns a Figure and Axes object.
data_output
intermediate
2:00remaining
How many Axes are created?
What is the number of Axes objects created by this code?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
print(len(axs.flatten()))
A4
B2
C1
D0
Attempts:
2 left
💡 Hint
The argument (2, 2) creates a grid of subplots.
🔧 Debug
advanced
2:00remaining
Identify the error in this code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax1, ax2 = fig.add_subplot(121)
ax3 = fig.add_subplot(122)
print(type(ax1))
ATypeError: 'int' object is not iterable
BAttributeError: 'Figure' object has no attribute 'add_subplot'
CValueError: not enough values to unpack (expected 2, got 1)
DNo error, prints <class 'matplotlib.axes._subplots.AxesSubplot'>
Attempts:
2 left
💡 Hint
Check the return value of add_subplot when called with a single argument.
visualization
advanced
2:00remaining
What does this plot look like?
Given this code, what is the layout of the subplots?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3)
for i, ax in enumerate(axs):
    ax.plot([0, 1], [i, i+1])
plt.show()
AA single empty plot
BThree plots stacked vertically
COne plot with three lines
DThree plots side by side horizontally
Attempts:
2 left
💡 Hint
The arguments (1, 3) create a grid with 1 row and 3 columns.
🧠 Conceptual
expert
3:00remaining
Understanding Figure and Axes relationship
Which statement correctly describes the relationship between Figure and Axes in matplotlib?
AA Figure can only contain one Axes object at a time.
BA Figure can contain multiple Axes, but each Axes belongs to exactly one Figure.
CEach Axes can belong to multiple Figures simultaneously.
DAxes and Figure are independent and unrelated objects.
Attempts:
2 left
💡 Hint
Think about how plots are organized inside a figure.