Marker size variation in Matplotlib - Time & Space Complexity
When we change marker sizes in a plot, the computer must draw each marker with its own size. We want to understand how the time to draw grows as we add more markers.
How does the drawing time increase when we vary marker sizes for many points?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1000)
y = np.random.rand(1000)
sizes = np.linspace(10, 200, 1000)
plt.scatter(x, y, s=sizes)
plt.show()
This code plots 1000 points with marker sizes changing from small to large.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each marker with its specific size.
- How many times: Once for each point, here 1000 times.
Each new point adds one more marker to draw, so the total work grows as we add points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 marker draws |
| 100 | 100 marker draws |
| 1000 | 1000 marker draws |
Pattern observation: The drawing time grows directly with the number of points.
Time Complexity: O(n)
This means the time to draw markers grows in a straight line as we add more points.
[X] Wrong: "Changing marker sizes makes the drawing time grow faster than the number of points."
[OK] Correct: Each marker is drawn once regardless of size; size changes do not multiply the number of drawing steps.
Understanding how drawing time grows with data size helps you explain performance in data visualization tasks clearly and confidently.
What if we added a loop to redraw each marker multiple times? How would the time complexity change?