We use plotting to see data clearly. Matplotlib offers two ways to make plots: the pyplot interface and the Axes interface. Knowing the difference helps you choose the best way to draw your charts.
Axes vs pyplot interface comparison in Matplotlib
# Pyplot interface example import matplotlib.pyplot as plt plt.plot(x, y) plt.title('Title') plt.show() # Axes interface example fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Title') plt.show()
The pyplot interface works like a simple drawing board where commands affect the current figure and axes.
The Axes interface gives you an object (ax) to control specific parts of the plot directly.
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] plt.plot(x, y) plt.title('Pyplot Interface') plt.show()
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Axes Interface') plt.show()
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) axs[0].plot([1, 2, 3], [4, 5, 6]) axs[0].set_title('Left Plot') axs[1].plot([1, 2, 3], [6, 5, 4]) axs[1].set_title('Right Plot') plt.show()
This program shows the same plot twice: once using the pyplot interface and once using the Axes interface. Both produce the same visual result but use different ways to control the plot.
import matplotlib.pyplot as plt # Data x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] # Using pyplot interface plt.plot(x, y, label='pyplot') plt.title('Pyplot Interface Plot') plt.xlabel('X axis') plt.ylabel('Y axis') plt.legend() plt.show() # Using Axes interface fig, ax = plt.subplots() ax.plot(x, y, label='axes') ax.set_title('Axes Interface Plot') ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.legend() plt.show()
The pyplot interface is easier for quick plots but less flexible for complex layouts.
The Axes interface is better for detailed control and multiple plots in one figure.
You can mix both, but it is best to stick to one style for clarity.
Pyplot interface is simple and good for quick plotting.
Axes interface gives more control and is better for complex figures.
Choose the interface based on your plotting needs and complexity.
Practice
pyplot interface and the Axes interface in matplotlib?Solution
Step 1: Understand pyplot interface
Thepyplotinterface is designed for quick and easy plotting with simple commands.Step 2: Understand Axes interface
TheAxesinterface provides detailed control over plot elements, useful for complex figures.Final Answer:
pyplotis simpler and good for quick plots, whileAxesoffers more control for complex figures. -> Option BQuick Check:
Interface complexity = pyplot simple, Axes detailed [OK]
- Confusing which interface is simpler
- Thinking both interfaces have same control level
- Assuming pyplot is only for 3D plots
Solution
Step 1: Recall syntax for creating figure and axes
The correct syntax to create a figure and axes object isfig, ax = plt.subplots().Step 2: Check other options
plt.figure()creates only a figure,plt.axes()creates axes but not figure, andplt.plot()is for plotting, not creating axes.Final Answer:
fig, ax = plt.subplots() -> Option AQuick Check:
Correct Axes creation = plt.subplots() [OK]
- Using plt.figure() expecting axes returned
- Confusing plt.axes() with plt.figure()
- Trying to assign plt.plot() to axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.title('My Plot')
plt.show()Solution
Step 1: Understand plotting with Axes
Theax.plot()draws the line on the axes object.Step 2: Understand plt.title() effect
plt.title()sets the title on the current axes, so it works here.Final Answer:
A plot with line connecting points (1,4), (2,5), (3,6) and title 'My Plot'. -> Option DQuick Check:
Axes plot + plt.title() = plot with title [OK]
- Thinking plt.title() won't work with Axes interface
- Expecting error due to mixing interfaces
- Assuming no plot will show
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.figure()
plt.plot([1, 2, 3], [4, 5, 6])
ax.set_title('Test Plot')
plt.show()Solution
Step 1: Check plotting method
plt.plot()plots on the current pyplot axes, not theaxcreated byplt.subplots().Step 2: Check title setting
ax.set_title()correctly sets the title on theaxaxes.Final Answer:
Using plt.plot() instead of ax.plot() causes the plot to appear on a different axes. -> Option CQuick Check:
Plotting on wrong axes = plt.plot() vs ax.plot() [OK]
- Confusing plt.plot() and ax.plot()
- Thinking ax.set_title() is invalid
- Missing plt.show() causing no output
Solution
Step 1: Understand subplot creation with shared axis
plt.subplots(1, 2, sharey=True)creates two axes side-by-side sharing the y-axis.Step 2: Plot on each axes separately
Plotting onax1andax2allows independent x-data but shared y-axis.Final Answer:
Usefig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)and plot onax1andax2. -> Option AQuick Check:
Shared y-axis subplots = plt.subplots with sharey=True [OK]
- Trying to share axis with plt.plot() only
- Creating separate figures instead of subplots
- Plotting both lines on one axes when separate plots needed
Ready to prove it?
Pass the test and this pattern moves unknown
