Unequal subplot sizes in Matplotlib - Time & Space Complexity
We want to understand how the time to create plots changes when we use subplots of different sizes in matplotlib.
How does the number and size of subplots affect the work matplotlib does?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.6, 0.8]) # Large subplot
ax2 = fig.add_axes([0.75, 0.1, 0.2, 0.3]) # Smaller subplot
ax1.plot(range(100))
ax2.plot(range(50))
plt.show()
This code creates a figure with two subplots of different sizes and plots data on each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting data points on each subplot.
- How many times: Once per data point in each subplot (100 points in first, 50 in second).
As the number of points to plot increases, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations per subplot |
| 100 | About 100 operations per subplot |
| 1000 | About 1000 operations per subplot |
Pattern observation: Doubling the number of points roughly doubles the work needed to plot.
Time Complexity: O(n)
This means the time to draw the plots grows linearly with the number of points plotted.
[X] Wrong: "Making one subplot bigger will make the whole plotting time much slower regardless of data size."
[OK] Correct: The size of the subplot affects drawing area but the main time cost comes from how many data points are plotted, not the subplot size alone.
Understanding how plotting time grows helps you explain performance in data visualization tasks clearly and confidently.
"What if we added a third subplot with twice as many points as the largest one? How would the time complexity change?"