Alpha transparency for overlap in Matplotlib - Time & Space Complexity
We want to understand how the time to draw plots with transparency changes as we add more points.
How does adding more overlapping points affect the drawing time?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(1000)
y = np.random.rand(1000)
plt.scatter(x, y, alpha=0.3)
plt.show()
This code plots 1000 points with some transparency to show overlapping areas.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each point with transparency on the plot.
- How many times: Once for each of the n points (here, 1000 times).
As we add more points, the drawing work grows roughly in direct proportion to the number of points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 drawing operations |
| 100 | 100 drawing operations |
| 1000 | 1000 drawing operations |
Pattern observation: The work grows linearly as we add more points.
Time Complexity: O(n)
This means the time to draw increases directly with the number of points plotted.
[X] Wrong: "Adding transparency makes the drawing time constant no matter how many points there are."
[OK] Correct: Each point still needs to be drawn, and transparency just changes how it looks, not how many times the drawing happens.
Understanding how drawing time grows helps you explain performance when visualizing large datasets.
What if we changed the alpha transparency to 1 (fully opaque)? How would the time complexity change?