Bar plots in Pandas - Time & Space Complexity
When we create bar plots using pandas, the time it takes depends on how much data we have.
We want to know how the work grows as we add more data to plot.
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.Series([5, 3, 9, 1, 7])
data.plot(kind='bar')
plt.show()
This code creates a bar plot from a pandas Series with 5 values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing one bar for each data point in the Series.
- How many times: Once per data point, so as many times as the number of values.
As the number of data points increases, the number of bars to draw grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 bars drawn |
| 100 | 100 bars drawn |
| 1000 | 1000 bars drawn |
Pattern observation: The work grows directly with the number of data points.
Time Complexity: O(n)
This means the time to create the bar plot grows in a straight line with the number of bars.
[X] Wrong: "Creating a bar plot takes the same time no matter how many bars there are."
[OK] Correct: Each bar needs to be drawn, so more bars mean more work and more time.
Understanding how plotting time grows helps you explain performance when working with data visualizations in real projects.
"What if we grouped the data before plotting? How would that affect the time complexity?"