Bird
Raised Fist0
Matplotlibdata~10 mins

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

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

Practice

(1/5)
1. What does the variable fig represent when you write fig, ax = plt.subplots() in matplotlib?
easy
A. The specific plot area where data is drawn
B. The whole figure container that holds all plot elements
C. A list of all plots in the figure
D. The title of the plot

Solution

  1. Step 1: Understand the role of fig in plt.subplots()

    The fig variable holds the entire figure container, which can include multiple plots, titles, legends, and other elements.
  2. Step 2: Differentiate fig from ax

    The ax variable is the specific plot area inside the figure where data is drawn, not the whole container.
  3. Final Answer:

    The whole figure container that holds all plot elements -> Option B
  4. Quick Check:

    fig = whole figure container [OK]
Hint: Remember: fig = full figure, ax = plot area [OK]
Common Mistakes:
  • Confusing fig with ax as the plot area
  • Thinking fig is just the plot title
  • Assuming fig is a list of plots
2. Which of the following is the correct syntax to create a figure and axes using matplotlib's plt.subplots()?
easy
A. plt.subplots = fig, ax
B. fig = plt.subplots(ax)
C. ax, fig = plt.subplots()
D. fig, ax = plt.subplots()

Solution

  1. Step 1: Recall the correct unpacking syntax

    The function plt.subplots() returns two objects: the figure and the axes. The correct way to capture them is fig, ax = plt.subplots().
  2. Step 2: Check each option for syntax correctness

    fig = plt.subplots(ax) tries to pass ax as an argument incorrectly. ax, fig = plt.subplots() reverses the order, which is wrong. plt.subplots = fig, ax tries to assign incorrectly.
  3. Final Answer:

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

    Correct unpacking = fig, ax = plt.subplots() [OK]
Hint: Remember: fig first, then ax from plt.subplots() [OK]
Common Mistakes:
  • Swapping fig and ax order
  • Passing ax as argument to plt.subplots()
  • Incorrect assignment syntax
3. What will be the output type of ax after running this code?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
medium
A.
B.
C.
D.

Solution

  1. Step 1: Understand what plt.subplots() returns

    The function returns a figure object and an axes object. The axes object is usually an instance of AxesSubplot class.
  2. Step 2: Identify the type of ax

    After unpacking, ax is the axes object, which is of type matplotlib.axes._subplots.AxesSubplot.
  3. Final Answer:

    <class 'matplotlib.axes._subplots.AxesSubplot'> -> Option A
  4. Quick Check:

    ax type = AxesSubplot [OK]
Hint: ax is the plot area object, not the figure [OK]
Common Mistakes:
  • Confusing ax type with fig type
  • Thinking ax is a list or tuple
  • Assuming ax is a string or number
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
fig, ax = plt.subplots
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
medium
A. fig and ax are reversed in assignment
B. ax.plot() arguments are incorrect
C. Missing parentheses after plt.subplots
D. plt.show() is called before plotting

Solution

  1. Step 1: Check the call to plt.subplots

    The code uses plt.subplots without parentheses, so it assigns the function itself, not its result.
  2. Step 2: Understand the impact on ax.plot

    Since fig, ax are assigned incorrectly, ax is a function, not an axes object, so ax.plot() will cause an error.
  3. Final Answer:

    Missing parentheses after plt.subplots -> Option C
  4. Quick Check:

    Call plt.subplots() with () [OK]
Hint: Always add () to call plt.subplots function [OK]
Common Mistakes:
  • Forgetting parentheses on plt.subplots
  • Mixing up fig and ax variables
  • Calling plt.show() too early
5. You want to create a figure with 2 rows and 1 column of plots using plt.subplots(). Which code correctly creates the figure and axes, and plots a line on the second subplot?
hard
A. fig, ax = plt.subplots(2, 1) ax[1].plot([1, 2, 3], [3, 2, 1])
B. fig, ax = plt.subplots(1, 2) ax[2].plot([1, 2, 3], [3, 2, 1])
C. fig, ax = plt.subplots(2, 1) ax.plot([1, 2, 3], [3, 2, 1])
D. fig, ax = plt.subplots(2) ax[0].plot([1, 2, 3], [3, 2, 1])

Solution

  1. Step 1: Create figure with 2 rows and 1 column

    Use plt.subplots(2, 1) to create 2 vertical subplots. This returns ax as an array of axes.
  2. Step 2: Plot on the second subplot

    Access the second subplot with ax[1] (indexing starts at 0) and call plot() on it.
  3. Final Answer:

    fig, ax = plt.subplots(2, 1) ax[1].plot([1, 2, 3], [3, 2, 1]) -> Option A
  4. Quick Check:

    2 rows, 1 col = plt.subplots(2,1), plot on ax[1] [OK]
Hint: Use ax[index] for multiple subplots, index starts at 0 [OK]
Common Mistakes:
  • Using wrong subplot shape (1,2) instead of (2,1)
  • Trying to plot on ax without indexing when multiple axes exist
  • Using invalid index like ax[2] when only 2 subplots exist
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