0
0
Matplotlibdata~20 mins

Small multiples (facet grid) in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Facet Grid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple facet grid plot
What will be the shape of the figure created by this code using matplotlib's subplots for small multiples?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
fig, axs = plt.subplots(2, 3)
for i, ax in enumerate(axs.flatten()):
    ax.plot(x, np.sin(x + i))
plt.close(fig)
print(fig.get_size_inches())
A[12.0 8.0]
B[18.0 12.0]
C[6.0 4.0]
D[8.0 6.0]
Attempts:
2 left
💡 Hint
The default figsize for plt.subplots is (6, 4) inches.
data_output
intermediate
1:30remaining
Number of subplots created
How many subplots will be created by this code snippet?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 4)
print(len(axs.flatten()))
A7
B12
C3
D4
Attempts:
2 left
💡 Hint
Count rows times columns.
visualization
advanced
2:00remaining
Identify the correct facet grid layout
Which option shows the correct arrangement of subplots for this code?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, axs = plt.subplots(2, 2)
for i, ax in enumerate(axs.flatten()):
    ax.plot(x, np.sin(x + i))
plt.close(fig)
# The question is about the layout of subplots in a 2x2 grid.
AFour plots arranged in 2 rows and 2 columns
BFour plots arranged in 1 row and 4 columns
CTwo plots arranged in 2 rows and 1 column
DThree plots arranged in 3 rows and 1 column
Attempts:
2 left
💡 Hint
Check the arguments of plt.subplots(2, 2).
🔧 Debug
advanced
1:30remaining
Error in facet grid subplot indexing
What error will this code raise?
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[2].plot([1, 2, 3], [4, 5, 6])
AIndexError: index 2 is out of bounds for axis 0 with size 2
BAttributeError: 'AxesSubplot' object has no attribute 'plot'
CTypeError: 'AxesSubplot' object is not subscriptable
DNo error, plot will be created
Attempts:
2 left
💡 Hint
axs is a 2D array of shape (2, 2).
🚀 Application
expert
2:30remaining
Creating a facet grid with shared axes
Which code snippet correctly creates a 3x2 facet grid with shared x and y axes using matplotlib?
Afig, axs = plt.subplots(3, 2, sharex=False, sharey=True)
Bfig, axs = plt.subplots(3, 2, sharex=True, sharey=False)
Cfig, axs = plt.subplots(2, 3, sharex=True, sharey=True)
Dfig, axs = plt.subplots(3, 2, sharex=True, sharey=True)
Attempts:
2 left
💡 Hint
Check the order of rows and columns and the sharex/sharey parameters.