0
0
Matplotlibdata~10 mins

Axes vs pyplot interface comparison in Matplotlib - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Axes vs pyplot interface comparison
Start
Choose Interface
pyplot Interface
Use plt.plot()
Figure & Axes auto-created
Show plot with plt.show()
Axes Interface
Create Figure & Axes explicitly
Use ax.plot() on Axes object
Show plot with plt.show()
You start by choosing either pyplot or Axes interface. Pyplot auto-creates figure and axes, while Axes requires explicit creation. Both end with showing the 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 uses the Axes interface: it creates a figure and axes, plots data on the axes, then shows the plot.
Execution Table
StepActionObject Created/UsedEffect/Output
1Call plt.subplots()Figure and Axes objectsFigure and Axes created explicitly
2Call ax.plot([1,2,3], [4,5,6])Axes objectLine plotted on Axes
3Call plt.show()Figure windowPlot window displayed
4End-Plotting complete
💡 Plot displayed and program ends
Variable Tracker
VariableStartAfter plt.subplots()After ax.plot()Final
figNoneFigure objectFigure objectFigure object
axNoneAxes objectAxes with line plottedAxes with line plotted
Key Moments - 2 Insights
Why do we need to create Figure and Axes explicitly in the Axes interface?
Because unlike pyplot interface which creates them automatically, the Axes interface requires you to create Figure and Axes objects to have control over where to plot. See execution_table step 1.
Can we plot multiple lines using pyplot interface without creating Axes?
Yes, pyplot manages Figure and Axes internally, so you can call plt.plot() multiple times and it will plot on the current Axes. This is different from Axes interface where you call plot on specific Axes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what objects are created by plt.subplots()?
AOnly Figure object
BOnly Axes object
CBoth Figure and Axes objects
DNo objects created
💡 Hint
See execution_table row 1 under 'Object Created/Used'
At which step is the line actually drawn on the plot in the execution_table?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at execution_table row 2 under 'Effect/Output'
If you use pyplot interface and call plt.plot() twice, what happens?
ATwo separate figures are created
BTwo lines are plotted on the same Axes
COnly the last line is shown
DAn error occurs
💡 Hint
Recall pyplot interface auto-manages Axes and plots on current Axes
Concept Snapshot
Axes vs pyplot interface comparison:
- Pyplot interface auto-creates Figure and Axes.
- Use plt.plot() to plot directly.
- Axes interface requires explicit creation: fig, ax = plt.subplots().
- Plot on Axes with ax.plot().
- Both end with plt.show() to display.
- Axes interface gives more control over plots.
Full Transcript
This lesson compares the two main ways to plot in matplotlib: the pyplot interface and the Axes interface. The pyplot interface is simple and quick. It automatically creates a figure and axes for you. You just call plt.plot() to draw lines. The Axes interface requires you to create the figure and axes objects explicitly using plt.subplots(). Then you call ax.plot() on the axes object to draw lines. Both methods end by calling plt.show() to display the plot window. The Axes interface gives you more control because you can manage multiple axes and figures easily. The execution table shows step-by-step what happens: first creating figure and axes, then plotting lines, then showing the plot. The variable tracker shows how the fig and ax variables change during execution. Key moments clarify why explicit creation is needed in the Axes interface and how pyplot manages axes automatically. The quiz tests understanding of object creation and plotting steps. The snapshot summarizes the differences clearly.