0
0
Matplotlibdata~10 mins

Fig, ax = plt.subplots pattern in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fig, ax = plt.subplots pattern
Call plt.subplots()
Create Figure object (fig)
Create Axes object(s) (ax)
Return fig, ax to variables
Use ax to plot data
Show or save figure
This flow shows how plt.subplots() creates a figure and axes, returns them, and then you use the axes to draw your plot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
This code creates a figure and axes, plots a line on the axes, and displays the plot.
Execution Table
StepActionResultVariable Assigned
1Call plt.subplots()Creates Figure and Axes objectsfig, ax
2Assign Figure object to figfig holds Figure instancefig
3Assign Axes object to axax holds Axes instanceax
4Call ax.plot([1,2,3],[4,5,6])Line plotted on axax (updated)
5Call plt.show()Displays the figure windowNone
💡 Plot displayed and program waits for user to close window
Variable Tracker
VariableStartAfter plt.subplots()After ax.plot()Final
figNoneFigure objectFigure objectFigure object
axNoneAxes objectAxes object with line plottedAxes object with line plotted
Key Moments - 3 Insights
Why do we assign plt.subplots() to two variables, fig and ax?
plt.subplots() returns two objects: the Figure (fig) which is the whole drawing area, and the Axes (ax) which is the part where data is drawn. Assigning both lets you control the plot and the container separately, as shown in execution_table step 1 and 2.
Can we plot directly with plt.plot() instead of using ax.plot()?
Yes, plt.plot() works but it uses the current axes implicitly. Using ax.plot() is clearer and better for multiple plots or subplots, as seen in execution_table step 4 where ax.plot() explicitly draws on the axes.
What happens if we call plt.subplots() without assigning to variables?
You create the figure and axes but lose the references to control them. You can't add plots or customize easily. This is why assignment in step 1 is important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of object is assigned to 'ax' after plt.subplots()?
AFigure object
BAxes object
CLine object
DPlot object
💡 Hint
Check execution_table row 3 where 'ax' is assigned.
At which step in the execution_table is the line actually drawn on the plot?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the action involving ax.plot() in the table.
If we skip assigning plt.subplots() to variables, what is the main consequence?
APlot will not display
BFigure will be empty but axes exist
CCannot customize or add plots easily
DProgram will crash
💡 Hint
Refer to key_moments explanation about losing references.
Concept Snapshot
plt.subplots() creates a Figure and Axes.
Assign them as fig, ax = plt.subplots().
Use ax to plot data (ax.plot()).
Figure (fig) is the whole window.
Axes (ax) is the drawing area.
Call plt.show() to display.
Full Transcript
The plt.subplots() function creates two objects: a Figure and an Axes. The Figure is the entire drawing area or window, while the Axes is the part where the actual data is drawn. When you write fig, ax = plt.subplots(), you save these objects into variables. Then you use ax.plot() to draw your data on the Axes. Finally, plt.show() displays the figure window with your plot. This pattern helps you control your plots clearly and is especially useful when you have multiple plots or want to customize your graph.