Storytelling with visualization sequence in Matplotlib - Time & Space Complexity
When we create a sequence of visualizations, each step adds work. We want to know how the time to draw all visuals grows as we add more steps.
How does adding more charts affect the total time to show the story?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
for i in range(n):
plt.figure()
plt.plot(range(100))
plt.title(f"Step {i+1}")
plt.show()
This code creates and shows n separate line charts, each with 100 points, one after another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop runs n times to create and show each plot.
- How many times: Exactly n times, once per visualization step.
Each new visualization adds a fixed amount of work. So, if we double the number of steps, the total work roughly doubles.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times the work to draw 10 charts |
| 100 | 100 times the work to draw 100 charts |
| 1000 | 1000 times the work to draw 1000 charts |
Pattern observation: The total work grows directly with the number of visualization steps.
Time Complexity: O(n)
This means the time to complete the storytelling grows in a straight line as we add more visual steps.
[X] Wrong: "Adding more charts won't affect total time much because each chart is simple."
[OK] Correct: Even simple charts take time, and doing many of them adds up linearly, so total time grows with the number of charts.
Understanding how adding steps affects total time helps you explain performance in real projects. It shows you can think about how work grows as data or visuals increase.
"What if each visualization step plotted data that grows with i (like i*100 points)? How would the time complexity change?"