0
0
Matplotlibdata~10 mins

Figure and Axes mental model in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Figure and Axes mental model
Create Figure
Add Axes to Figure
Plot data on Axes
Render Figure with Axes and Plot
Show or Save Figure
This flow shows how a Figure is created first, then Axes are added to it, data is plotted on the Axes, and finally the Figure is rendered and displayed or saved.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plot_line = ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
This code creates a Figure, adds one Axes, plots a simple line, and displays the plot.
Execution Table
StepActionObject CreatedState ChangeOutput/Effect
1Call plt.figure()Figure objectFigure created, emptyFigure instance ready
2Call fig.add_subplot(111)Axes objectAxes added to FigureAxes instance linked to Figure
3Call plot_line = ax.plot([1,2,3],[4,5,6])Line2D objectLine plotted on AxesLine data stored in Axes
4Call plt.show()NoneFigure rendered with Axes and plotPlot window displayed
5EndNoneNo further actionsExecution complete
💡 Plot displayed and execution ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
figNoneFigure instanceFigure instance with AxesFigure instance with Axes and plotFigure instance rendered
axNoneNoneAxes instanceAxes with plot dataAxes rendered
plot_lineNoneNoneNoneLine2D instanceLine rendered
Key Moments - 3 Insights
Why do we need both Figure and Axes objects?
The Figure is the whole window or page for the plot, while Axes is the area where data is drawn. The execution_table shows Figure created first (Step 1), then Axes added inside it (Step 2), so both are needed for a complete plot.
What happens if we plot without adding Axes explicitly?
If you plot without Axes, matplotlib creates default Axes inside the Figure. But here, Step 2 shows explicit Axes creation, which gives more control. The variable_tracker shows 'ax' is None before Step 2, so no plot can happen without Axes.
Does plt.show() create or modify objects?
No, plt.show() just renders the current Figure and Axes. Step 4 in execution_table shows no new objects created, only rendering happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what object is created at Step 2?
AFigure object
BLine2D object
CAxes object
DNone
💡 Hint
Check the 'Object Created' column at Step 2 in execution_table
At which step does the plot line get added to the Axes?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'State Change' columns in execution_table for when plotting happens
If we skip Step 2 (adding Axes), what would happen when calling ax.plot()?
AError because ax is None
BPlot appears normally
CFigure is created twice
DPlot is saved automatically
💡 Hint
Check variable_tracker for 'ax' value before Step 2 and understand if ax.plot() can run
Concept Snapshot
Figure and Axes are core matplotlib objects.
Figure is the whole drawing area.
Axes is the plot area inside Figure.
Create Figure first, then add Axes.
Plot data on Axes.
Show or save Figure to display.
Full Transcript
This lesson shows how matplotlib uses Figure and Axes objects to create plots. First, a Figure is created as the main container. Then, Axes are added inside the Figure to hold the plot. Data is plotted on the Axes. Finally, the Figure is rendered and displayed. The execution table traces each step: creating Figure, adding Axes, plotting data, and showing the plot. Variables like fig and ax change state as objects are created and linked. Key points include understanding the roles of Figure and Axes, and that plt.show() only renders the plot without creating new objects. The visual quiz tests understanding of these steps and object creation.