Cumulative histograms help you see how data adds up step by step. They show the total count or frequency up to each point.
0
0
Cumulative histograms in Matplotlib
Introduction
To understand how many values fall below a certain number in your data.
When you want to compare the overall distribution of two or more groups.
To check the percentage of data points below a threshold.
When you want to visualize the running total of data occurrences.
To analyze trends in data accumulation over a range.
Syntax
Matplotlib
plt.hist(data, bins=number_of_bins, cumulative=True)The cumulative=True option makes the histogram add counts up to each bin.
You can also use density=True to show proportions instead of counts.
Examples
Creates a cumulative histogram with 10 bins.
Matplotlib
plt.hist(data, bins=10, cumulative=True)
Shows a cumulative histogram with proportions instead of counts.
Matplotlib
plt.hist(data, bins=20, cumulative=True, density=True)
Uses custom bin edges for the cumulative histogram.
Matplotlib
plt.hist(data, bins=[0, 1, 2, 3, 4], cumulative=True)
Sample Program
This program creates 1000 random numbers from a normal distribution and plots their cumulative histogram with 30 bins. The plot shows how many data points fall below each value.
Matplotlib
import matplotlib.pyplot as plt import numpy as np # Generate random data np.random.seed(0) data = np.random.randn(1000) # Plot cumulative histogram plt.hist(data, bins=30, cumulative=True, color='skyblue', edgecolor='black') plt.title('Cumulative Histogram of Random Data') plt.xlabel('Value') plt.ylabel('Cumulative Count') plt.grid(True) plt.show()
OutputSuccess
Important Notes
Cumulative histograms always increase or stay the same as you move right.
You can combine cumulative with density to see cumulative percentages.
Setting bins controls the detail level of the histogram.
Summary
Cumulative histograms show running totals of data counts.
Use cumulative=True in plt.hist() to create them.
They help understand data distribution and thresholds easily.