Scatter plots in Data Analysis Python - Time & Space Complexity
When we create scatter plots, we want to know how the time to draw the plot changes as we add more points.
We ask: How does the work grow when the number of data points grows?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
def draw_scatter(x, y):
plt.scatter(x, y)
plt.show()
This code draws a scatter plot using two lists of numbers, x and y, representing points.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting each point on the graph.
- How many times: Once for each data point in the input lists.
As the number of points increases, the time to draw grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Doubling the points roughly doubles the work needed to plot.
Time Complexity: O(n)
This means the time to draw the scatter plot grows linearly with the number of points.
[X] Wrong: "Adding more points won't affect the drawing time much because the plot size stays the same."
[OK] Correct: Even if the plot area is fixed, each point still needs to be drawn, so more points mean more work.
Understanding how plotting time grows helps you explain performance when working with large datasets in real projects.
"What if we used a sampling method to plot only a subset of points? How would the time complexity change?"