0
0
Matplotlibdata~10 mins

Nested subplots with subfigures in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested subplots with subfigures
Create Figure
Add Subfigure 1
Add Subplots in Subfigure 1
Add Subfigure 2
Add Subplots in Subfigure 2
Plot data in each subplot
Show Figure with nested subplots
Start by creating a main figure, then add subfigures inside it. Each subfigure can have its own grid of subplots. Finally, plot data in each subplot and display the figure.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
subfigs = fig.subfigures(1, 2)
ax1 = subfigs[0].subplots(2, 1)
ax2 = subfigs[1].subplots(1, 2)
ax1[0].plot([1, 2], [3, 4])
plt.show()
This code creates a figure with two subfigures side by side. The first subfigure has two rows of subplots, the second has two columns. It plots a simple line in the first subplot.
Execution Table
StepActionResult/StateNotes
1Create main figureFigure object createdEmpty figure ready
2Add 2 subfigures horizontallyTwo subfigure objects inside main figureSubfigures split figure into left and right parts
3Create 2-row subplots in left subfigureArray of 2 Axes objects in left subfigureVertical stack of plots
4Create 2-column subplots in right subfigureArray of 2 Axes objects in right subfigureHorizontal side-by-side plots
5Plot line on first subplot of left subfigureLine appears on ax1[0]Data plotted on first subplot
6Show figureWindow with nested subplots appearsAll subplots visible with layout
7EndExecution stopsAll plotting done
💡 All subplots created and plotted, figure displayed, program ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
figNoneFigure objectFigure objectFigure objectFigure objectFigure object
subfigsNoneArray of 2 subfiguresArray of 2 subfiguresArray of 2 subfiguresArray of 2 subfiguresArray of 2 subfigures
ax1NoneNoneArray of 2 Axes (vertical)Array of 2 Axes (vertical)Array of 2 Axes (vertical) with line on ax1[0]Array of 2 Axes (vertical) with line on ax1[0]
ax2NoneNoneNoneArray of 2 Axes (horizontal)Array of 2 Axes (horizontal)Array of 2 Axes (horizontal)
Key Moments - 3 Insights
Why do we create subfigures before subplots?
Subfigures act like containers inside the main figure. We add subplots inside these containers to organize plots in groups. See execution_table steps 2 and 3.
Can subfigures have different subplot layouts?
Yes, each subfigure can have its own grid size and shape. For example, left subfigure has 2 rows, right subfigure has 2 columns (execution_table steps 3 and 4).
What happens if we plot before creating subplots?
You need Axes objects to plot on. Without subplots, there is no place to draw. So plotting happens after subplots are created (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker table. What is the value of 'ax1' after Step 3?
AArray of 2 Axes arranged vertically
BArray of 2 Axes arranged horizontally
CNone
DSingle Axes object
💡 Hint
Check the 'ax1' row under 'After Step 3' column in variable_tracker
According to the execution_table, at which step do we add subplots to the right subfigure?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step describing subplots creation in the right subfigure in execution_table
If we skip creating subfigures and add subplots directly to the figure, how would the execution_table change?
AStep 5 would be missing
BNo change, subfigures are optional
CStep 2 would be missing, subplots created directly on figure
DFigure creation would fail
💡 Hint
Refer to execution_table step 2 about subfigure creation
Concept Snapshot
Nested subplots with subfigures:
- Create main figure with plt.figure()
- Add subfigures using fig.subfigures(rows, cols)
- Add subplots inside each subfigure with subfig.subplots(rows, cols)
- Plot data on each subplot Axes
- Show figure with plt.show()
Allows grouping plots visually inside one figure.
Full Transcript
This lesson shows how to create nested subplots using subfigures in matplotlib. First, a main figure is created. Then, subfigures are added inside the figure to divide it into sections. Each subfigure can have its own grid of subplots. We create subplots inside each subfigure and plot data on them. Finally, the figure is displayed showing all nested plots. Variables like fig, subfigs, ax1, and ax2 track the figure, subfigures, and subplot axes. Key moments include understanding why subfigures come before subplots and how each subfigure can have different layouts. The visual quiz tests understanding of variable states and execution steps. This method helps organize complex figures with grouped plots.