0
0
Matplotlibdata~10 mins

Why multiple plots per figure matter in Matplotlib - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why multiple plots per figure matter
Start: Create Figure
Add Subplot 1
Plot Data 1
Add Subplot 2
Plot Data 2
Add Subplot 3
Plot Data 3
Adjust Layout
Show Figure with Multiple Plots
End
We start by creating a figure, then add multiple subplots to it, each with its own data. Finally, we adjust layout and display all plots together.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
plt.show()
This code creates a figure with 4 subplots arranged in 2 rows and 2 columns, then plots different data on two of them and shows the figure.
Execution Table
StepActionResultFigure State
1Create figure and 2x2 subplotsFigure with 4 empty subplots created4 empty plots in grid (2 rows x 2 cols)
2Plot data [1,4,9] on subplot (0,0)Line plot appears on top-left subplotTop-left subplot has line plot, others empty
3Plot data [1,2,3] on subplot (0,1)Line plot appears on top-right subplotTop-left and top-right subplots have plots, bottom two empty
4Show figureFigure window opens showing 2 plots in 4-subplot gridAll subplots visible; 2 with data, 2 empty
5EndExecution completeFigure displayed with multiple plots
💡 All plotting commands executed; figure shown with multiple plots in one window
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
figNoneFigure object with 4 subplotsSameSameSame
axsNone2x2 array of subplot axesTop-left subplot has plotTop-left and top-right subplots have plotsSame
Key Moments - 2 Insights
Why do we use multiple subplots instead of separate figures?
Using multiple subplots in one figure groups related plots together for easy comparison, as shown in execution_table steps 2 and 3 where plots appear side by side.
What happens if we plot on the wrong subplot index?
Plotting on a wrong subplot index will place the plot in an unexpected location or cause an error; execution_table step 2 and 3 show correct indexing for top-left and top-right plots.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the figure contain after Step 3?
ATwo plots on the top row subplots
BOnly one plot on the top-left subplot
CFour plots, one in each subplot
DNo plots, all subplots empty
💡 Hint
Refer to the 'Figure State' column in Step 3 of execution_table
At which step does the figure window open to show the plots?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Action' column for when plt.show() is called in execution_table
If we add a plot to axs[1,1], how would the variable 'axs' change after Step 3?
AIt would cause an error
BIt would remain unchanged with two plots
CIt would have three subplots with plots
DIt would remove existing plots
💡 Hint
Look at variable_tracker for how 'axs' updates when plots are added
Concept Snapshot
Use plt.subplots(rows, cols) to create multiple plots in one figure.
Each subplot is accessed by axs[row, col].
Plot data on each subplot separately.
Shows related data together for easy comparison.
Call plt.show() once to display all plots.
Full Transcript
This visual execution shows how to create multiple plots in one figure using matplotlib. First, a figure with a grid of subplots is created. Then, data is plotted on specific subplots by indexing the axes array. The figure is displayed with all plots visible together. This helps compare data side by side in one window. Variables 'fig' and 'axs' track the figure and subplot axes. Key moments include why grouping plots matters and how subplot indexing works. The quiz tests understanding of figure state and plotting steps.