Broken axes concept in Matplotlib - Time & Space Complexity
We want to understand how the time to draw a plot with broken axes changes as the data size grows.
How does matplotlib handle the drawing steps when axes are broken?
Analyze the time complexity of the following matplotlib code snippet.
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
fig = plt.figure(figsize=(6,4))
bax = brokenaxes(ylims=((0, 1), (10, 11)), fig=fig)
x = range(100)
y = [i**2 for i in x]
bax.plot(x, y)
plt.show()
This code creates a plot with two y-axis ranges broken apart and plots 100 points.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting each data point on the broken axes.
- How many times: Once for each data point (100 times here).
- Additional operation: Drawing two separate axis areas for the broken y-limits.
As the number of points increases, the plotting work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~10 drawing steps |
| 100 | ~100 drawing steps |
| 1000 | ~1000 drawing steps |
Pattern observation: Doubling the data roughly doubles the drawing work.
Time Complexity: O(n)
This means the time to draw grows linearly with the number of points plotted.
[X] Wrong: "Broken axes make plotting twice as slow no matter what."
[OK] Correct: The extra axes add some fixed overhead, but the main cost depends on how many points you plot, not just the broken axes.
Understanding how plotting scales helps you explain performance when visualizing large datasets, a useful skill in data science roles.
What if we added more broken axis sections? How would that affect the time complexity?