Bird
Raised Fist0
Matplotlibdata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Figure represent in matplotlib's Figure and Axes model?
easy
A. A single data point on the plot
B. The area where data points are drawn
C. The entire image or window that contains all plots
D. The color scheme of the plot

Solution

  1. Step 1: Understand the role of Figure

    The Figure is the whole container or window that holds one or more plots.
  2. Step 2: Differentiate Figure from Axes

    Axes is the part inside the Figure where data is drawn, not the entire window.
  3. Final Answer:

    The entire image or window that contains all plots -> Option C
  4. Quick Check:

    Figure = whole window [OK]
Hint: Figure is the whole plot window, not just the data area [OK]
Common Mistakes:
  • Confusing Figure with Axes
  • Thinking Figure is a single plot line
  • Mixing Figure with plot colors
2. Which of the following is the correct way to create a Figure and Axes in matplotlib?
easy
A. fig, ax = plt.subplots()
B. fig = plt.figure(), ax = plt.plot()
C. ax = plt.figure(), fig = plt.axes()
D. plt.create_figure(), plt.create_axes()

Solution

  1. Step 1: Recall matplotlib syntax

    The standard way to create Figure and Axes together is using plt.subplots().
  2. Step 2: Check each option

    fig, ax = plt.subplots() correctly assigns Figure and Axes from plt.subplots(). Others misuse functions or assign incorrectly.
  3. Final Answer:

    fig, ax = plt.subplots() -> Option A
  4. Quick Check:

    Use plt.subplots() to get Figure and Axes [OK]
Hint: Use plt.subplots() to get both Figure and Axes [OK]
Common Mistakes:
  • Using plt.plot() to create Axes
  • Confusing plt.figure() with plt.subplots()
  • Trying non-existent functions like plt.create_figure()
3. What will be the output type of the variable ax after running this code?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
medium
A. A matplotlib Figure object
B. A matplotlib Axes object
C. A list of Axes objects
D. A NumPy array

Solution

  1. Step 1: Understand plt.subplots() return values

    It returns a tuple: (Figure object, Axes object or array of Axes).
  2. Step 2: Check variable assignment

    Here, ax is assigned the second element, which is a single Axes object.
  3. Final Answer:

    A matplotlib Axes object -> Option B
  4. Quick Check:

    ax = Axes object from plt.subplots() [OK]
Hint: plt.subplots() returns (Figure, Axes) tuple [OK]
Common Mistakes:
  • Thinking ax is a Figure
  • Assuming ax is a list without multiple plots
  • Confusing Axes with NumPy arrays
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
medium
A. plt.axes() should be called on the Figure object
B. plt.axes() creates a new Figure, not Axes
C. plt.figure() must be called after plt.axes()
D. No error, code runs fine

Solution

  1. Step 1: Understand plt.figure() and plt.axes()

    plt.figure() creates a Figure and sets it as current. plt.axes() creates Axes in the current Figure using default positioning.
  2. Step 2: Verify code execution

    The plot is correctly added to ax and plt.show() displays the figure. The code runs without errors.
  3. Final Answer:

    No error, code runs fine -> Option D
  4. Quick Check:

    plt.axes() adds to current Figure [OK]
Hint: plt.figure() sets current figure for plt.axes() [OK]
Common Mistakes:
  • Calling plt.axes() without linking to Figure
  • Assuming plt.axes() auto-links to last Figure
  • Calling plt.figure() after plt.axes()
5. You want to create a figure with two side-by-side plots sharing the same y-axis. Which code correctly uses the Figure and Axes model to achieve this?
hard
A. fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4])
B. fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.sharey(ax2)
C. fig, ax = plt.subplots(2, 1) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4])
D. fig, ax = plt.subplots(1, 2) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4])

Solution

  1. Step 1: Understand subplots with shared axes

    Using plt.subplots(1, 2, sharey=True) creates one row, two columns of Axes sharing the y-axis.
  2. Step 2: Check code correctness

    fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]): fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]) correctly unpacks two Axes and plots data with shared y-axis. fig, ax = plt.subplots(1, 2) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) omits sharey=True. fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.sharey(ax2) calls non-existent sharey() method. fig, ax = plt.subplots(2, 1) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) creates vertical subplots.
  3. Final Answer:

    fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]) -> Option A
  4. Quick Check:

    Use sharey=True in plt.subplots() for shared y-axis [OK]
Hint: Use sharey=True in plt.subplots() for shared y-axis [OK]
Common Mistakes:
  • Trying to call sharey() on Axes objects
  • Creating vertical subplots instead of horizontal
  • Not unpacking multiple Axes correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard