Figure and Axes mental model in Matplotlib - Time & Space Complexity
When working with matplotlib, it is helpful to understand how the figure and axes objects behave in terms of time cost.
We want to know how the time to create and update plots grows as we add more axes or figures.
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 3)
for ax in axs.flat:
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
This code creates a 3 by 3 grid of axes and plots a simple line on each axes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each axes to plot data.
- How many times: Once for each axes, here 9 times (3 rows x 3 columns).
As the number of axes increases, the time to plot on each axes grows linearly.
| Input Size (number of axes) | Approx. Operations |
|---|---|
| 10 | 10 plotting calls |
| 100 | 100 plotting calls |
| 1000 | 1000 plotting calls |
Pattern observation: Doubling the number of axes roughly doubles the work done.
Time Complexity: O(n)
This means the time to create and plot on axes grows directly with the number of axes.
[X] Wrong: "Adding more axes does not affect plotting time much because each plot is simple."
[OK] Correct: Even simple plots take time, and plotting on many axes adds up linearly.
Understanding how plotting time grows with the number of axes helps you design efficient visualizations and manage resources well.
"What if we changed the code to plot multiple lines on each axes? How would the time complexity change?"
Practice
Figure represent in matplotlib's Figure and Axes model?Solution
Step 1: Understand the role of Figure
The Figure is the whole container or window that holds one or more plots.Step 2: Differentiate Figure from Axes
Axes is the part inside the Figure where data is drawn, not the entire window.Final Answer:
The entire image or window that contains all plots -> Option CQuick Check:
Figure = whole window [OK]
- Confusing Figure with Axes
- Thinking Figure is a single plot line
- Mixing Figure with plot colors
Solution
Step 1: Recall matplotlib syntax
The standard way to create Figure and Axes together is usingplt.subplots().Step 2: Check each option
fig, ax = plt.subplots() correctly assigns Figure and Axes fromplt.subplots(). Others misuse functions or assign incorrectly.Final Answer:
fig, ax = plt.subplots() -> Option AQuick Check:
Use plt.subplots() to get Figure and Axes [OK]
- Using plt.plot() to create Axes
- Confusing plt.figure() with plt.subplots()
- Trying non-existent functions like plt.create_figure()
ax after running this code?import matplotlib.pyplot as plt fig, ax = plt.subplots()
Solution
Step 1: Understand plt.subplots() return values
It returns a tuple: (Figure object, Axes object or array of Axes).Step 2: Check variable assignment
Here,axis assigned the second element, which is a single Axes object.Final Answer:
A matplotlib Axes object -> Option BQuick Check:
ax = Axes object from plt.subplots() [OK]
- Thinking ax is a Figure
- Assuming ax is a list without multiple plots
- Confusing Axes with NumPy arrays
import matplotlib.pyplot as plt fig = plt.figure() ax = plt.axes() ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
Solution
Step 1: Understand plt.figure() and plt.axes()
plt.figure()creates a Figure and sets it as current.plt.axes()creates Axes in the current Figure using default positioning.Step 2: Verify code execution
The plot is correctly added toaxandplt.show()displays the figure. The code runs without errors.Final Answer:
No error, code runs fine -> Option DQuick Check:
plt.axes() adds to current Figure [OK]
- Calling plt.axes() without linking to Figure
- Assuming plt.axes() auto-links to last Figure
- Calling plt.figure() after plt.axes()
Solution
Step 1: Understand subplots with shared axes
Usingplt.subplots(1, 2, sharey=True)creates one row, two columns of Axes sharing the y-axis.Step 2: Check code correctness
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]):fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4])correctly unpacks two Axes and plots data with shared y-axis. fig, ax = plt.subplots(1, 2) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) omitssharey=True. fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.sharey(ax2) calls non-existentsharey()method. fig, ax = plt.subplots(2, 1) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) creates vertical subplots.Final Answer:
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]) -> Option AQuick Check:
Use sharey=True in plt.subplots() for shared y-axis [OK]
- Trying to call sharey() on Axes objects
- Creating vertical subplots instead of horizontal
- Not unpacking multiple Axes correctly
Ready to prove it?
Pass the test and this pattern moves unknown
