0
0
Matplotlibdata~5 mins

Broken axes concept in Matplotlib - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Broken axes concept
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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.

Final Time Complexity

Time Complexity: O(n)

This means the time to draw grows linearly with the number of points plotted.

Common Mistake

[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.

Interview Connect

Understanding how plotting scales helps you explain performance when visualizing large datasets, a useful skill in data science roles.

Self-Check

What if we added more broken axis sections? How would that affect the time complexity?