Subplots for multiple charts in Pandas - Time & Space Complexity
When we create multiple charts using subplots in pandas, we want to know how the time to draw them changes as we add more charts.
We ask: How does the work grow when we increase the number of charts?
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame({
'A': range(100),
'B': range(100, 200),
'C': range(200, 300)
})
fig, axes = plt.subplots(3, 1)
for i, col in enumerate(data.columns):
data[col].plot(ax=axes[i])
plt.show()
This code creates three line charts stacked vertically, one for each column in the data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each column to draw a chart.
- How many times: Once for each column (number of charts).
Each new chart adds roughly the same amount of work to draw it.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 charts | 3 times the work of one chart |
| 10 charts | 10 times the work of one chart |
| 100 charts | 100 times the work of one chart |
Pattern observation: The work grows directly in proportion to the number of charts.
Time Complexity: O(n)
This means if you double the number of charts, the time to draw them roughly doubles.
[X] Wrong: "Drawing multiple charts together is as fast as drawing just one chart."
[OK] Correct: Each chart requires its own drawing steps, so more charts mean more work and more time.
Understanding how drawing multiple charts scales helps you explain performance in data visualization tasks clearly and confidently.
What if we changed the code to plot all columns on the same single chart instead of separate subplots? How would the time complexity change?