Histogram plots in Pandas - Time & Space Complexity
When we create histogram plots using pandas, we want to know how the time to draw the plot changes as the data grows.
We ask: How does the work needed to build the histogram grow with more data?
Analyze the time complexity of the following code snippet.
import pandas as pd
import numpy as np
# Create a DataFrame with one column of random numbers
data = pd.DataFrame({"values": np.random.randn(1000)})
# Plot histogram with 10 bins
hist = data["values"].plot.hist(bins=10)
This code creates a histogram plot of 1000 random numbers divided into 10 bins.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Counting how many data points fall into each bin.
- How many times: Each of the n data points is checked once to find its bin.
As the number of data points grows, the time to count them into bins grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: Doubling the data roughly doubles the work needed to build the histogram.
Time Complexity: O(n)
This means the time to create the histogram grows linearly with the number of data points.
[X] Wrong: "The number of bins affects the time complexity a lot, so more bins means much slower."
[OK] Correct: The number of bins usually stays small and fixed, so it does not grow with data size. The main work is checking each data point once, so bins have little effect on overall time.
Understanding how data size affects plotting speed helps you explain performance in real projects. It shows you can think about how code scales, a useful skill beyond just making charts.
"What if we increased the number of bins to grow with the data size? How would the time complexity change?"