0
0
Matplotlibdata~10 mins

Plt.subplots with rows and columns in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Plt.subplots with rows and columns
Call plt.subplots(rows, cols)
Create Figure object
Create Axes array with shape (rows, cols)
Return Figure and Axes
User plots on each Axes
Display or save the figure
The function plt.subplots creates a figure and a grid of subplots (axes) arranged in rows and columns, returning them for plotting.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 3)
axs[0, 0].plot([1, 2], [3, 4])
plt.show()
This code creates a 2-row by 3-column grid of plots and draws a line on the first subplot.
Execution Table
StepActionResultNotes
1Call plt.subplots(2, 3)Figure and 2x3 Axes array createdFigure is empty, axes ready for plotting
2Access axs[0, 0]First subplot Axes objectTop-left plot in grid
3Plot line on axs[0, 0]Line appears on first subplotData points (1,3) and (2,4) connected
4Call plt.show()Figure window opens with 6 subplotsOnly first subplot has a line, others are empty
5EndExecution stopsAll plots displayed
💡 All subplots created and displayed, program ends after plt.show()
Variable Tracker
VariableStartAfter Step 1After Step 3Final
figNoneFigure objectFigure objectFigure object
axsNone2x3 numpy array of Axes2x3 numpy array of Axes with line on axs[0,0]Same as after step 3
Key Moments - 3 Insights
Why is axs a 2D array and not a list?
Because plt.subplots with rows and columns returns a numpy array shaped (rows, cols), so axs[0,0] accesses the first subplot. See execution_table step 2.
What happens if you try axs[3,0] in this example?
It causes an IndexError because axs only has 2 rows (indices 0 and 1). See variable_tracker for axs shape.
Why do only some subplots show data after plotting?
Because we only plotted on axs[0,0]. The other subplots are empty by default. See execution_table step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shape of axs after step 1?
A1D array with 6 Axes
BList of 2 Axes
C2D array with shape (2, 3)
DSingle Axes object
💡 Hint
Check execution_table row 1 under Result describing axs shape
At which step does the first line appear on the plot?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
See execution_table step 3 where plotting happens
If you change plt.subplots(2, 3) to plt.subplots(3, 2), how does axs shape change?
Aaxs becomes 1D array
BShape becomes (3, 2)
CShape stays (2, 3)
Daxs becomes a list
💡 Hint
The rows and columns arguments define axs shape as (rows, cols)
Concept Snapshot
plt.subplots(rows, cols) creates a Figure and a grid of Axes with given rows and columns.
Returns (fig, axs) where axs is a 2D array of Axes.
Access subplots by axs[row, col].
Plot on each Axes independently.
Call plt.show() to display all subplots.
Full Transcript
This visual execution trace shows how plt.subplots with rows and columns works. First, plt.subplots(2, 3) creates a figure and a 2 by 3 grid of subplot axes. The axs variable holds these axes in a 2D array. We then plot a line on the first subplot axs[0, 0]. Calling plt.show() opens a window displaying all six subplots, with only the first showing the line. The variable tracker confirms fig and axs states after each step. Key moments clarify common confusions about axs shape and indexing. The quiz tests understanding of axs shape, plotting step, and effect of changing rows and columns. The snapshot summarizes the main points for quick reference.