Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using small multiples (facet grid) in matplotlib?
easy
A. To display multiple related charts side by side for easy comparison
B. To create a single large plot with multiple lines
C. To animate a plot over time
D. To change the color scheme of a plot

Solution

  1. Step 1: Understand the concept of small multiples

    Small multiples are multiple small charts arranged in a grid to compare different groups or categories easily.
  2. Step 2: Identify the purpose in matplotlib

    Matplotlib uses small multiples to show many charts side by side, making it easier to compare data visually.
  3. Final Answer:

    To display multiple related charts side by side for easy comparison -> Option A
  4. Quick Check:

    Small multiples = multiple charts side by side [OK]
Hint: Small multiples = many small charts for comparison [OK]
Common Mistakes:
  • Confusing small multiples with animations
  • Thinking it changes colors only
  • Assuming it creates one big plot
2. Which of the following is the correct way to create a 2x2 grid of subplots in matplotlib?
easy
A. fig, axes = plt.subplots(4)
B. fig, axes = plt.subplots(1, 4)
C. fig, axes = plt.subplots(2, 2)
D. fig, axes = plt.subplots(2)

Solution

  1. Step 1: Recall plt.subplots() syntax

    plt.subplots(rows, columns) creates a grid of subplots with given rows and columns.
  2. Step 2: Match the grid size

    To create a 2x2 grid, use plt.subplots(2, 2).
  3. Final Answer:

    fig, axes = plt.subplots(2, 2) -> Option C
  4. Quick Check:

    plt.subplots(2, 2) = 2 rows and 2 columns [OK]
Hint: Use plt.subplots(rows, columns) for grid size [OK]
Common Mistakes:
  • Using single number for grid shape
  • Confusing rows and columns
  • Missing one dimension in arguments
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3)
data = [1, 2, 3]
for i, ax in enumerate(axes):
    ax.plot([data[i]] * 3)
plt.show()
medium
A. Three line plots each with three points of the same value 1, 2, and 3 respectively
B. A single plot with three lines of values 1, 2, and 3
C. Error because axes is not iterable
D. Three scatter plots with points 1, 2, and 3

Solution

  1. Step 1: Understand plt.subplots(1, 3)

    This creates 1 row and 3 columns of subplots, so axes is an array of 3 axes objects.
  2. Step 2: Loop plots each subplot

    For each axis, it plots a line with three points all equal to data[i] (1, then 2, then 3).
  3. Final Answer:

    Three line plots each with three points of the same value 1, 2, and 3 respectively -> Option A
  4. Quick Check:

    Loop over axes plots lines with repeated values [OK]
Hint: Loop over axes to plot each subplot separately [OK]
Common Mistakes:
  • Thinking axes is a single plot
  • Assuming error due to axes type
  • Confusing line plot with scatter plot
4. Identify the error in this code for creating a 2x2 grid of plots:
fig, axes = plt.subplots(2, 2)
for i in range(4):
    axes[i].plot([1, 2, 3])
plt.show()
medium
A. The plot data list is invalid
B. plt.subplots(2, 2) creates only 2 plots, not 4
C. plt.show() is missing
D. axes is a 2D array, so axes[i] indexing causes an error

Solution

  1. Step 1: Understand axes shape from plt.subplots(2, 2)

    axes is a 2x2 numpy array of Axes objects, not a flat list.
  2. Step 2: Identify indexing error

    axes[i] tries to index a 2D array with one index, causing an error. Correct is axes[row, col] or flatten axes first.
  3. Final Answer:

    axes is a 2D array, so axes[i] indexing causes an error -> Option D
  4. Quick Check:

    2D axes need two indices or flatten before looping [OK]
Hint: 2D axes need two indices or flatten before indexing [OK]
Common Mistakes:
  • Assuming axes is 1D array
  • Ignoring error from wrong indexing
  • Thinking plt.show() is missing
5. You have a dataset with sales data for 3 regions. How would you create a 1-row, 3-column grid of plots showing sales trends for each region separately using matplotlib?
hard
A. Use plt.plot() three times without subplots
B. Use plt.subplots(1, 3), loop over axes, and plot each region's data on each subplot
C. Use plt.subplots(3, 1) and plot all regions on each subplot
D. Use plt.subplots(1, 1) and plot all regions on the same plot

Solution

  1. Step 1: Create a 1x3 grid for three regions

    Use plt.subplots(1, 3) to create one row and three columns of plots.
  2. Step 2: Loop over axes and plot each region's data

    Loop through each axis and plot the sales data for each region separately to compare trends side by side.
  3. Final Answer:

    Use plt.subplots(1, 3), loop over axes, and plot each region's data on each subplot -> Option B
  4. Quick Check:

    One row, three columns, loop to plot each region [OK]
Hint: Create grid then loop axes to plot groups separately [OK]
Common Mistakes:
  • Plotting all data on one plot
  • Using wrong grid shape
  • Not looping over axes