Nested subplots with subfigures in Matplotlib - Time & Space Complexity
When creating nested subplots with subfigures in matplotlib, the time to draw the figure depends on how many plots we make.
We want to know how the drawing time grows as we add more subplots inside subfigures.
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
fig = plt.figure(constrained_layout=True)
subfigs = fig.subfigures(2, 1)
for subfig in subfigs:
axs = subfig.subplots(2, 2)
for ax in axs.flat:
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
This code creates a figure with 2 subfigures stacked vertically. Each subfigure has a 2x2 grid of subplots, and each subplot draws a simple line plot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting on each subplot inside nested loops.
- How many times: Outer loop runs 2 times (for subfigures), inner loop runs 4 times (for subplots), total 8 plots.
As the number of subfigures and subplots per subfigure increases, the total plots grow by multiplying these counts.
| Input Size (n = subplots per subfigure) | Approx. Operations (plots) |
|---|---|
| 2 subfigures, 2x2 subplots (4) | 8 |
| 2 subfigures, 10x10 subplots (100) | 200 |
| 3 subfigures, 20x20 subplots (400) | 1200 |
Pattern observation: Total plots grow by multiplying number of subfigures and number of subplots per subfigure.
Time Complexity: O(m * n^2)
This means the time grows proportionally to the number of subfigures (m) times the square of the number of subplots per subfigure (n), since subplots are arranged in an n x n grid.
[X] Wrong: "Adding more subplots inside subfigures only adds a small fixed time, so complexity stays the same."
[OK] Correct: Each subplot requires drawing operations, so total time grows with the total number of subplots, not fixed.
Understanding how nested plotting scales helps you reason about performance in data visualization tasks, a useful skill in many projects.
"What if we changed the number of subfigures dynamically based on data size? How would the time complexity change?"
Practice
fig.subfigures() in matplotlib?Solution
Step 1: Understand the role of
This method creates subfigures, which are large groups inside a main figure.fig.subfigures()Step 2: Recognize the use of subfigures
Each subfigure can contain its own grid of smaller plots, helping organize complex layouts.Final Answer:
To create large groups inside a figure that can hold their own smaller plots -> Option BQuick Check:
Subfigures = large groups inside figure [OK]
- Thinking subfigures add only one plot
- Confusing subfigures with saving figures
- Assuming subfigures change plot colors
Solution
Step 1: Understand subfigures layout parameters
The parameters (rows, columns) define the grid of subfigures. For side by side, use 1 row and 2 columns.Step 2: Match parameters to options
fig.subfigures(1, 2) usesfig.subfigures(1, 2), which creates 1 row and 2 columns, placing subfigures side by side.Final Answer:
fig.subfigures(1, 2) -> Option CQuick Check:
Side by side = 1 row, 2 columns [OK]
- Swapping rows and columns
- Using subplots() instead of subfigures()
- Choosing more rows than needed
fig = plt.figure() subfigs = fig.subfigures(2, 1) ax1 = subfigs[0].subplots(1, 3) ax2 = subfigs[1].subplots(2, 2) print(ax1.shape, ax2.shape)
Solution
Step 1: Analyze subfigures and their subplot grids
First subfigure has 1 row and 3 columns, so ax1 shape is (1, 3). Second subfigure has 2 rows and 2 columns, so ax2 shape is (2, 2).Step 2: Confirm shapes printed
Printing ax1.shape and ax2.shape outputs (1, 3) and (2, 2) respectively.Final Answer:
(1, 3) (2, 2) -> Option DQuick Check:
Subplots shape = (rows, columns) [OK]
- Confusing flat arrays with 2D arrays
- Mixing up rows and columns
- Assuming subfigures affect shape differently
fig = plt.figure() subfigs = fig.subfigures(1, 2) ax = subfigs.subplots(2, 2)
Solution
Step 1: Understand the type of subfigs
fig.subfigures(1, 2) returns a list or array of subfigure objects, not a single subfigure.Step 2: Correct method call for subplots
We must call subplots() on each individual subfigure, not on the entire list subfigs.Final Answer:
subfigs.subplots() should be called on each subfigure, not on the list -> Option AQuick Check:
Call subplots() on single subfigure [OK]
- Calling subplots() on the list of subfigures
- Assuming subfigures() needs 3 args
- Thinking subfigures can't have subplots
Solution
Step 1: Set subfigures layout vertically
Two subfigures stacked vertically means 2 rows and 1 column, sofig.subfigures(2, 1).Step 2: Create subplots inside each subfigure
Top subfigure gets a 2x2 grid, bottom subfigure gets a single plot (1x1). Call subplots() on each subfigure individually.Final Answer:
fig = plt.figure() subfigs = fig.subfigures(2, 1) axs_top = subfigs[0].subplots(2, 2) axs_bottom = subfigs[1].subplots(1, 1) -> Option AQuick Check:
Vertical stack = (2,1), call subplots() on each subfigure [OK]
- Using (1,2) for vertical stacking
- Calling subplots() on the list of subfigures
- Not matching subplot grids to subfigures
Ready to prove it?
Pass the test and this pattern moves unknown
