0
0
Data Analysis Pythondata~10 mins

Figure and axes creation in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Figure and axes creation
Import matplotlib.pyplot
Create Figure object
Add Axes or Subplots to Figure
Use Axes to plot data
Show or save the Figure
This flow shows how to import the plotting library, create a figure, add axes to it, plot data on the axes, and finally display or save the figure.
Execution Sample
Data Analysis Python
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
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 window.
Execution Table
StepActionObject CreatedState/ValueOutput/Effect
1Import matplotlib.pyplot as pltplt moduleAvailable for useNo visual output
2Create figure with plt.figure()Figure objectEmpty figure with no axesNo visual output
3Add subplot 111 to figureAxes objectOne axes added to figureNo visual output
4Plot data on axesLine2D objectLine plotted on axesNo visual output
5Call plt.show()N/AFigure window opensPlot window displays line graph
6End of scriptN/AAll objects remain in memoryPlot window remains open until closed
💡 Script ends after showing the plot window
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
pltNot definedmatplotlib.pyplot modulematplotlib.pyplot modulematplotlib.pyplot modulematplotlib.pyplot module
figNot definedFigure object (empty)Figure object (with 1 axes)Figure object (with 1 axes and line)Figure object (with 1 axes and line)
axNot definedNot definedAxes object (subplot 111)Axes object (with line plotted)Axes object (with line plotted)
Key Moments - 3 Insights
Why do we need to create a Figure before adding Axes?
The Figure is the main container for plots. Without it, Axes have no place to live. See execution_table step 2 and 3 where Figure is created first, then Axes are added.
What does '111' mean in add_subplot(111)?
It means 1 row, 1 column, and this is the 1st subplot. This creates a single axes filling the figure. See execution_table step 3.
Why doesn't plotting data immediately show the plot?
Plotting adds data to the Axes but does not display it. plt.show() must be called to open the plot window. See execution_table step 4 vs step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what object is created at step 3?
AFigure object
BLine2D object
CAxes object
Dplt module
💡 Hint
Check the 'Object Created' column at step 3 in the execution_table.
At which step does the plot window appear?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look at the 'Output/Effect' column to find when the figure window opens.
If we skip creating the Figure object, what happens when adding Axes?
AError because no Figure exists
BAxes are added to a default figure automatically
CAxes are created but not shown
DPlot shows without axes
💡 Hint
The code calls fig.add_subplot(111), so 'fig' must be defined first; skipping causes NameError: name 'fig' is not defined.
Concept Snapshot
import matplotlib.pyplot as plt
fig = plt.figure()  # Create a figure container
ax = fig.add_subplot(111)  # Add one axes (1 row, 1 col, 1st plot)
ax.plot(x, y)  # Plot data on axes
plt.show()  # Display the figure window
Key: Figure holds Axes; Axes hold plots; show() displays all.
Full Transcript
This lesson shows how to create a figure and axes using matplotlib in Python. First, we import matplotlib.pyplot as plt. Then we create a Figure object with plt.figure(). Next, we add an Axes object to the figure using fig.add_subplot(111), which means one row, one column, first subplot. We plot data on the axes with ax.plot(). Finally, plt.show() opens a window displaying the plot. Variables plt, fig, and ax change as we create and add objects. Beginners often wonder why Figure must be created first, what '111' means, and why plotting doesn't immediately show the plot. The execution table traces each step and object creation. The visual quiz tests understanding of these steps. Remember: Figure is the container, Axes is the plot area, and show() displays the figure.