0
0
Matplotlibdata~10 mins

Figure-level methods vs axes-level in Matplotlib - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Figure-level methods vs axes-level
Start: Create Figure and Axes
Use Figure-level method
Plot affects whole Figure
Use Axes-level method
Plot affects only one Axes
Show or save Figure
First, create a figure and axes. Figure-level methods affect the whole figure, while axes-level methods affect only one subplot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.suptitle('Figure Title')
ax.set_title('Axes Title')
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
This code creates a figure and one axes, sets a title for the figure and the axes, plots data on the axes, and shows the plot.
Execution Table
StepActionTargetEffectOutput
1Create figure and axesfig, axFigure and Axes objects createdfig and ax ready
2Set figure titlefig.suptitle('Figure Title')Title set for whole figureFigure title displayed
3Set axes titleax.set_title('Axes Title')Title set for single axesAxes title displayed
4Plot dataax.plot([1, 2, 3], [1, 4, 9])Line plot on axesLine visible on axes
5Show plotplt.show()Display figure windowFigure with titles and plot shown
6End-All commands executedPlot window open
💡 All plotting commands executed and figure displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
figNoneFigure object createdFigure title setFigure title setFigure title setFigure with title
axNoneAxes object createdAxes object unchangedAxes title setLine plot addedAxes with title and plot
Key Moments - 2 Insights
Why does fig.suptitle set the title for the whole figure but ax.set_title only sets the title for one subplot?
fig.suptitle applies to the entire figure container, affecting all subplots inside it, while ax.set_title applies only to the specific axes object, so it affects only that subplot. See execution_table steps 2 and 3.
If I call ax.plot multiple times, does it affect the whole figure or just one axes?
Each ax.plot call adds data only to that specific axes, not the whole figure. The figure contains all axes but plotting commands on axes are local. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the effect of step 2 (fig.suptitle)?
APlots data on the axes
BSets the title for the entire figure
CSets the title for one axes only
DCreates a new axes
💡 Hint
Check the 'Effect' column in step 2 of the execution_table
At which step is the line plot added to the axes?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Plot data' in the execution_table
If you want to add a title to only one subplot, which method should you use?
Aax.set_title()
Bfig.suptitle()
Cplt.title()
Dfig.set_title()
💡 Hint
Refer to the difference between figure-level and axes-level methods in the key_moments
Concept Snapshot
Figure-level methods (like fig.suptitle) affect the whole figure and all subplots.
Axes-level methods (like ax.set_title, ax.plot) affect only one subplot.
Create figure and axes with plt.subplots().
Use fig.suptitle() for overall title.
Use ax.set_title() and ax.plot() for individual subplot control.
Full Transcript
This lesson shows the difference between figure-level and axes-level methods in matplotlib. First, you create a figure and axes using plt.subplots(). Then, figure-level methods like fig.suptitle() set titles or properties for the entire figure, affecting all subplots. Axes-level methods like ax.set_title() and ax.plot() affect only the specific subplot (axes) they are called on. The execution table traces each step: creating figure and axes, setting titles, plotting data, and showing the figure. Variable tracking shows how fig and ax objects change after each step. Key moments clarify common confusions about scope of titles and plotting. The visual quiz tests understanding of which methods affect figure vs axes. This helps beginners see how to control overall figure appearance versus individual subplot details.