0
0
Matplotlibdata~10 mins

Cumulative histograms in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cumulative histograms
Start with data array
Calculate histogram bins
Count data points per bin
Calculate cumulative sum of counts
Plot cumulative histogram
End
The flow shows how data is binned, counts are accumulated cumulatively, and then plotted as a cumulative histogram.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.array([1,2,2,3,4,5,5,5,6])
plt.hist(data, bins=6, cumulative=True)
plt.show()
This code plots a cumulative histogram of the data array with 6 bins.
Execution Table
StepData Points ProcessedBin EdgesCounts per BinCumulative CountsPlot Action
1[][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][0, 0, 0, 0, 0, 0][0, 0, 0, 0, 0, 0]Initialize bins and counts
2[1][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 0, 0, 0, 0, 0][1, 0, 0, 0, 0, 0]Count 1 in first bin
3[1,2,2][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 0, 0, 0, 0][1, 3, 3, 3, 3, 3]Count two 2s in second bin, cumulative sums updated
4[1,2,2,3][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 1, 0, 0, 0][1, 3, 4, 4, 4, 4]Count 3 in third bin, cumulative sums updated
5[1,2,2,3,4][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 1, 1, 0, 0][1, 3, 4, 5, 5, 5]Count 4 in fourth bin, cumulative sums updated
6[1,2,2,3,4,5,5,5][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 1, 1, 3, 0][1, 3, 4, 5, 8, 8]Count three 5s in fifth bin, cumulative sums updated
7[1,2,2,3,4,5,5,5,6][1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 1, 1, 3, 1][1, 3, 4, 5, 8, 9]Count 6 in last bin, cumulative sums updated
8All data processed[1.0, 1.83, 2.67, 3.5, 4.33, 5.17, 6.0][1, 2, 1, 1, 3, 1][1, 3, 4, 5, 8, 9]Plot cumulative histogram and show
💡 All data points counted and cumulative sums calculated; histogram plotted.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
data processed[][1][1,2,2][1,2,2,3][1,2,2,3,4][1,2,2,3,4,5,5,5][1,2,2,3,4,5,5,5,6][1,2,2,3,4,5,5,5,6][1,2,2,3,4,5,5,5,6]
counts per bin[0,0,0,0,0,0][1,0,0,0,0,0][1,2,0,0,0,0][1,2,1,0,0,0][1,2,1,1,0,0][1,2,1,1,3,0][1,2,1,1,3,1][1,2,1,1,3,1][1,2,1,1,3,1]
cumulative counts[0,0,0,0,0,0][1,0,0,0,0,0][1,3,3,3,3,3][1,3,4,4,4,4][1,3,4,5,5,5][1,3,4,5,8,8][1,3,4,5,8,9][1,3,4,5,8,9][1,3,4,5,8,9]
Key Moments - 2 Insights
Why do cumulative counts keep increasing even if some bins have zero counts?
Because cumulative counts add all previous bin counts, zeros do not reduce the sum but keep it the same, as shown in execution_table rows 5 and 6.
Why does the last bin count increase when data points equal the upper edge?
Matplotlib includes the right edge in the last bin, so data points equal to the max value fall into the last bin, as seen in step 7 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the cumulative count for the second bin?
A1
B3
C2
D4
💡 Hint
Check the 'Cumulative Counts' column at step 3 in execution_table.
At which step does the cumulative count for the last bin first increase?
AStep 4
BStep 5
CStep 7
DStep 6
💡 Hint
Look at the 'Cumulative Counts' column for the last bin in execution_table rows.
If the data array had no values equal to 6, how would the final cumulative count for the last bin change?
AIt would be 4 instead of 9
BIt would be 5 instead of 9
CIt would remain 9
DIt would be 0
💡 Hint
Refer to variable_tracker for counts per bin and consider the last bin counts.
Concept Snapshot
Cumulative histograms count data points in bins and add counts cumulatively.
Use plt.hist(data, bins, cumulative=True) to plot.
Bins divide data range; cumulative sums show running totals.
Useful to see distribution accumulation over range.
Full Transcript
This visual execution traces how a cumulative histogram is created using matplotlib. Starting with a data array, bins are defined to split the data range. Each data point is counted into its bin. Then counts are summed cumulatively from the first bin to the last. The cumulative counts are plotted as a histogram that shows how data accumulates across bins. The execution table shows each step of counting and summing. The variable tracker follows how counts and cumulative sums change after processing each data point. Key moments clarify why cumulative sums increase even with zero counts in some bins and why the last bin includes the maximum data value. The quiz questions test understanding of cumulative counts at specific steps and effects of data changes. The snapshot summarizes the concept and usage in a few lines.