Figure-level methods vs axes-level in Matplotlib - Performance Comparison
When using matplotlib, we can draw plots using figure-level or axes-level methods. Understanding how the time to draw grows with data size helps us choose the right method.
We want to know: how does the time to create plots change as we add more data points?
Analyze the time complexity of the following matplotlib code snippets.
import matplotlib.pyplot as plt
import numpy as np
n = 100 # example size
x = np.linspace(0, 10, n)
y = np.sin(x)
# Figure-level method
plt.plot(x, y)
# Axes-level method
fig, ax = plt.subplots()
ax.plot(x, y)
This code plots a line chart with n data points using two approaches: figure-level and axes-level.
Look for repeated work done as data size grows.
- Primary operation: Drawing each data point on the plot.
- How many times: Once per data point, so n times.
As we add more points, the drawing work grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 drawing steps |
| 100 | 100 drawing steps |
| 1000 | 1000 drawing steps |
Pattern observation: Doubling data roughly doubles the work needed to draw.
Time Complexity: O(n)
This means the time to draw grows linearly with the number of data points.
[X] Wrong: "Figure-level methods are always slower because they handle more stuff."
[OK] Correct: Both figure-level and axes-level methods spend most time drawing data points, so their time grows similarly with data size.
Knowing how plotting time grows helps you explain choices in data visualization tasks clearly and confidently.
What if we added multiple lines to the same axes? How would the time complexity change?
Practice
Solution
Step 1: Understand figure-level vs axes-level
Figure-level methods control the whole figure, axes-level methods control individual plots.Step 2: Identify method for main figure title
The methodfig.suptitle()sets the main title for the entire figure, whileax.set_title()sets title for one subplot.Final Answer:
fig.suptitle() -> Option DQuick Check:
Figure main title = fig.suptitle() [OK]
- Using ax.set_title() for main figure title
- Using plt.title() which affects current axes
- Confusing xlabel with title
Solution
Step 1: Identify the object controlling axis labels
Axis labels belong to individual plots, so axes-level methods are used.Step 2: Choose correct method and object
The methodset_xlabel()is called on an axes object, soax.set_xlabel('X Label')is correct.Final Answer:
ax.set_xlabel('X Label') -> Option CQuick Check:
Axis label = ax.set_xlabel() [OK]
- Calling set_xlabel() on figure object
- Using plt.set_xlabel() which does not exist
- Confusing axis label with figure title
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.suptitle('Main Title')
ax.set_title('Plot Title')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
plt.show()Solution
Step 1: Understand figure and axes titles
fig.suptitle()sets the main figure title;ax.set_title()sets the subplot title.Step 2: Analyze axis labels and display
ax.set_xlabel()andax.set_ylabel()label the x and y axes of the plot. The code will show all these correctly.Final Answer:
A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes -> Option AQuick Check:
fig.suptitle + ax.set_title + ax labels = A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes [OK]
- Thinking fig.suptitle replaces ax.set_title
- Expecting error when using both titles
- Ignoring axis labels
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.set_title('Main Title')
ax.set_xlabel('X Label')
plt.show()Solution
Step 1: Check figure method usage
The figure object does not have a method calledset_title(). The correct method for figure title issuptitle().Step 2: Verify other method calls
ax.set_xlabel()is correct for labeling x-axis.plt.show()is correctly placed at the end.Final Answer:
fig.set_title() is not a valid method for figure objects -> Option AQuick Check:
Figure title method = fig.suptitle() not fig.set_title() [OK]
- Using fig.set_title() instead of fig.suptitle()
- Confusing axes methods with figure methods
- Calling plt.show() too early
Solution
Step 1: Create subplots and set main figure title
plt.subplots(1, 2)creates two side-by-side plots.fig.suptitle()sets the main figure title.Step 2: Set individual subplot titles correctly
Each subplot is accessed byaxs[0]andaxs[1]. Useset_title()to set their titles.Step 3: Verify other options are incorrect
The other options misuse methods likefig.set_title()orfig.title()which do not exist or use wrong subplot layout.Final Answer:
fig, axs = plt.subplots(1, 2) fig.suptitle('Main Title') axs[0].set_title('Left Plot') axs[1].set_title('Right Plot') plt.show() -> Option BQuick Check:
Figure main title = fig.suptitle(), subplot titles = axs[i].set_title() [OK]
- Using fig.set_title() instead of fig.suptitle()
- Calling title() instead of set_title()
- Wrong subplot layout dimensions
Ready to prove it?
Pass the test and this pattern moves unknown
