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?
Practice
broken axes in matplotlib plots?Solution
Step 1: Understand the concept of broken axes
Broken axes are used to skip over parts of the axis where data is not important or has large gaps.Step 2: Identify the main purpose
This helps to focus on important data ranges without showing the entire axis continuously.Final Answer:
To skip unimportant data ranges and focus on key parts -> Option AQuick Check:
Broken axes = skip gaps [OK]
- Thinking broken axes add 3D or animation
- Confusing broken axes with color changes
- Assuming broken axes zoom in automatically
brokenaxes package to use brokenaxes(...) directly for creating broken axes plots?Solution
Step 1: Recall the correct import syntax
Thebrokenaxespackage provides thebrokenaxesfunction to create broken axes plots.Step 2: Identify the correct import statement
The correct way isfrom brokenaxes import brokenaxesto access the function directly.Final Answer:
from brokenaxes import brokenaxes -> Option CQuick Check:
Import brokenaxes function = from brokenaxes import brokenaxes [OK]
- Using capital 'BrokenAxes' instead of 'brokenaxes'
- Trying to import from matplotlib instead of brokenaxes
- Using wrong import syntax causing ImportError
from brokenaxes import brokenaxes import matplotlib.pyplot as plt bax = brokenaxes(ylims=((0, 2), (8, 10))) bax.plot([0, 1, 2, 3], [1, 1.5, 9, 9.5]) plt.show()
Solution
Step 1: Analyze the ylims parameter
Theylims=((0, 2), (8, 10))means the y-axis is broken to show two ranges: 0 to 2 and 8 to 10, skipping 2 to 8.Step 2: Understand the plotted points
The y-values are 1, 1.5, 9, and 9.5, which fit into the two visible y-axis ranges.Final Answer:
A plot with y-axis broken between 2 and 8 showing points near 1 and 9 -> Option AQuick Check:
ylims tuple of ranges = broken y-axis [OK]
- Assuming y-axis is continuous without breaks
- Confusing x-axis and y-axis breaks
- Expecting an error from tuple ylims
from brokenaxes import brokenaxes import matplotlib.pyplot as plt bax = brokenaxes(xlims=(0, 2), (5, 7)) bax.plot([1, 6], [10, 20]) plt.show()
Solution
Step 1: Check the xlims argument syntax
The code usesxlims=(0, 2), (5, 7)which is invalid syntax; it should be a single tuple containing tuples likexlims=((0, 2), (5, 7)).Step 2: Confirm other parts are correct
The plot call and plt.show() are correct; the error is only in xlims syntax.Final Answer:
xlims should be a tuple of tuples, not separate tuples -> Option DQuick Check:
xlims syntax = tuple of tuples [OK]
- Passing multiple tuples without enclosing in one tuple
- Mixing up xlims and ylims syntax
- Calling plt.show() too early
brokenaxes?Solution
Step 1: Identify the axis to break
The gap is in y-values, so the y-axis needs to be broken to skip the large gap between 3 and 50.Step 2: Set ylims with two ranges
Usingylims=((0, 3), (50, 55))creates a broken y-axis showing both clusters clearly.Final Answer:
Use ylims=((0, 3), (50, 55)) when creating brokenaxes -> Option BQuick Check:
Broken y-axis = ylims tuple of ranges [OK]
- Breaking x-axis instead of y-axis
- Using single continuous ylims hiding gap
- Plotting separate figures unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
