0
0
Matplotlibdata~20 mins

Plt.subplots with rows and columns in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the shape of the axes array?
Consider this code snippet using plt.subplots with 3 rows and 2 columns. What is the shape of the axes object?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 2)
print(axes.shape)
A(2, 3)
B()
C(6,)
D(3, 2)
Attempts:
2 left
💡 Hint
The axes is a 2D array matching rows and columns.
data_output
intermediate
1:00remaining
How many axes objects are created?
What is the total number of axes objects created by plt.subplots(4, 1)?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4, 1)
print(len(axes))
A4
B1
C5
D0
Attempts:
2 left
💡 Hint
Number of axes equals rows times columns.
visualization
advanced
2:00remaining
Identify the subplot layout from the plot
You run this code to create subplots and plot data. Which layout matches the figure shown?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 3)
for i, ax in enumerate(axes.flat):
    ax.plot(x, np.sin(x + i))
plt.show()
A2 rows and 3 columns
B3 rows and 2 columns
C1 row and 6 columns
D6 rows and 1 column
Attempts:
2 left
💡 Hint
Check how many rows and columns are passed to plt.subplots.
🔧 Debug
advanced
1:30remaining
Why does this code raise an error?
This code tries to plot on subplots but raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
axes[2].plot([1, 2, 3])
plt.show()
AValueError because plot data is invalid
BTypeError because axes is not subscriptable
CIndexError because axes is a 2D array and index 2 is invalid
DNo error, code runs fine
Attempts:
2 left
💡 Hint
Check the shape of axes and how indexing works.
🚀 Application
expert
2:00remaining
How to flatten axes for iteration?
You create subplots with 3 rows and 3 columns. You want to loop over all axes in one loop. Which code correctly flattens the axes array?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 3)
# Which line below correctly flattens axes for iteration?
A
for ax in axes.flat:
    ax.plot([1, 2, 3])
B
for ax in axes.flatten():
    ax.plot([1, 2, 3])
C
for ax in axes.ravel():
    ax.plot([1, 2, 3])
D
for ax in axes:
    ax.plot([1, 2, 3])
Attempts:
2 left
💡 Hint
Check the methods available on numpy arrays for flattening.