Small multiples (facet grid) in Matplotlib - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When creating small multiples with matplotlib, we want to know how the time to draw charts grows as we add more plots.
How does adding more small charts affect the total work matplotlib does?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(3, 3)
for i, ax in enumerate(axs.flat):
x = np.linspace(0, 10, 100)
y = np.sin(x + i)
ax.plot(x, y)
plt.show()
This code creates a 3 by 3 grid of small plots, each showing a sine wave shifted by a different amount.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each subplot to draw a line plot.
- How many times: Once for each subplot, here 9 times (3 rows x 3 columns).
Each additional small plot adds roughly the same amount of work to draw its line.
| Input Size (number of plots) | Approx. Operations |
|---|---|
| 10 | 10 times the work to draw one plot |
| 100 | 100 times the work to draw one plot |
| 1000 | 1000 times the work to draw one plot |
Pattern observation: The total work grows directly with the number of small plots.
Time Complexity: O(n)
This means the time to draw all small multiples grows linearly as you add more plots.
[X] Wrong: "Adding more small plots only slightly increases drawing time because they are small."
[OK] Correct: Each plot still requires its own drawing steps, so total time adds up directly with the number of plots.
Understanding how drawing many small charts scales helps you design efficient visualizations and explain performance trade-offs clearly.
What if we changed the number of points in each plot from 100 to 1000? How would the time complexity change?
Practice
Solution
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.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.Final Answer:
To display multiple related charts side by side for easy comparison -> Option AQuick Check:
Small multiples = multiple charts side by side [OK]
- Confusing small multiples with animations
- Thinking it changes colors only
- Assuming it creates one big plot
Solution
Step 1: Recall plt.subplots() syntax
plt.subplots(rows, columns) creates a grid of subplots with given rows and columns.Step 2: Match the grid size
To create a 2x2 grid, use plt.subplots(2, 2).Final Answer:
fig, axes = plt.subplots(2, 2) -> Option CQuick Check:
plt.subplots(2, 2) = 2 rows and 2 columns [OK]
- Using single number for grid shape
- Confusing rows and columns
- Missing one dimension in arguments
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()Solution
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.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).Final Answer:
Three line plots each with three points of the same value 1, 2, and 3 respectively -> Option AQuick Check:
Loop over axes plots lines with repeated values [OK]
- Thinking axes is a single plot
- Assuming error due to axes type
- Confusing line plot with scatter plot
fig, axes = plt.subplots(2, 2)
for i in range(4):
axes[i].plot([1, 2, 3])
plt.show()Solution
Step 1: Understand axes shape from plt.subplots(2, 2)
axes is a 2x2 numpy array of Axes objects, not a flat list.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.Final Answer:
axes is a 2D array, so axes[i] indexing causes an error -> Option DQuick Check:
2D axes need two indices or flatten before looping [OK]
- Assuming axes is 1D array
- Ignoring error from wrong indexing
- Thinking plt.show() is missing
Solution
Step 1: Create a 1x3 grid for three regions
Use plt.subplots(1, 3) to create one row and three columns of plots.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.Final Answer:
Use plt.subplots(1, 3), loop over axes, and plot each region's data on each subplot -> Option BQuick Check:
One row, three columns, loop to plot each region [OK]
- Plotting all data on one plot
- Using wrong grid shape
- Not looping over axes
