Histogram vs bar chart distinction in Matplotlib - Performance Comparison
We want to understand how the time to draw histograms and bar charts changes as the data size grows.
How does the number of data points affect the drawing time for each chart type?
Analyze the time complexity of this matplotlib code for histogram and bar chart.
import matplotlib.pyplot as plt
import numpy as np
# Data
values = np.random.randint(1, 100, size=1000)
# Histogram
plt.hist(values, bins=20)
plt.show()
# Bar chart
counts = np.bincount(values)
plt.bar(range(len(counts)), counts)
plt.show()
This code draws a histogram with 20 bins and a bar chart showing counts of each value.
Look at what repeats when drawing these charts.
- Primary operation: Counting data points per bin or category.
- How many times: For histogram, data is scanned once to assign bins; for bar chart, counts are computed for each unique value.
As data size grows, the counting step takes longer because more points need to be processed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~10 counts |
| 100 | ~100 counts |
| 1000 | ~1000 counts |
Pattern observation: The counting work grows roughly in direct proportion to the number of data points.
Time Complexity: O(n)
This means the time to draw either chart grows linearly with the number of data points.
[X] Wrong: "Histograms are slower because they have bins and bar charts are faster because they just plot counts."
[OK] Correct: Both need to count data points; the difference is how counts are grouped, but counting still scales with data size.
Understanding how data size affects plotting helps you explain performance in data visualization tasks clearly and confidently.
"What if we increased the number of bins in the histogram? How would that affect the time complexity?"