Highlight and annotate pattern in Matplotlib - Time & Space Complexity
We want to understand how the time to highlight and annotate patterns in a plot changes as the data grows.
How does the work increase when we add more points to highlight or annotate?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
x = range(100)
y = [i**2 for i in x]
plt.plot(x, y)
for i in range(len(x)):
if y[i] > 2000:
plt.annotate('High', (x[i], y[i]), color='red')
plt.show()
This code plots a curve and adds annotations to points where the y-value is greater than 2000.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all points to check and annotate.
- How many times: Once for each data point (n times).
As the number of points increases, the code checks each point once and may add annotations.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and some annotations |
| 100 | About 100 checks and more annotations |
| 1000 | About 1000 checks and many annotations |
Pattern observation: The work grows roughly in direct proportion to the number of points.
Time Complexity: O(n)
This means the time to highlight and annotate grows linearly as the number of points increases.
[X] Wrong: "Adding annotations only takes constant time regardless of data size."
[OK] Correct: Each point must be checked and possibly annotated, so time grows with data size.
Understanding how plotting and annotation scale helps you write efficient data visualizations and explain your reasoning clearly.
"What if we only annotate points above a fixed threshold without checking all points? How would the time complexity change?"