0
0
Matplotlibdata~10 mins

Bin count and bin edges in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bin count and bin edges
Start with data array
Choose number of bins or bin edges
Calculate bin edges
Count data points in each bin
Output bin counts and edges
End
We start with data, decide bins, calculate edges, count points per bin, then output counts and edges.
Execution Sample
Matplotlib
import numpy as np

values = np.array([1, 2, 2, 3, 4, 5])
counts, edges = np.histogram(values, bins=3)
print(counts)
print(edges)
This code counts how many values fall into 3 bins and shows the bin edges.
Execution Table
StepActionInput/ConditionResult
1Start with data arrayvalues = [1, 2, 2, 3, 4, 5]Data array ready
2Choose number of binsbins = 33 bins selected
3Calculate bin edgesmin=1, max=5, bins=3Edges = [1.0, 2.3333, 3.6667, 5.0]
4Count data points in binsValues: 1,2,2,3,4,5Counts = [3, 1, 2]
5Output counts and edgescounts, edgescounts=[3,1,2], edges=[1.0,2.3333,3.6667,5.0]
6EndAll steps doneExecution complete
💡 All data points counted in bins, process finished
Variable Tracker
VariableStartAfter Step 3After Step 4Final
values[1,2,2,3,4,5][1,2,2,3,4,5][1,2,2,3,4,5][1,2,2,3,4,5]
binsNot set333
edgesNot set[1.0, 2.3333, 3.6667, 5.0][1.0, 2.3333, 3.6667, 5.0][1.0, 2.3333, 3.6667, 5.0]
countsNot setNot set[3, 1, 2][3, 1, 2]
Key Moments - 2 Insights
Why are there 4 bin edges for 3 bins?
Because bin edges mark the boundaries between bins, so for N bins there are N+1 edges (see execution_table step 3).
Why does the first bin count include the value 1 but not 2.3333?
Bins include the left edge but exclude the right edge except the last bin includes both edges (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the bin edge at index 2 after step 3?
A2.3333
B5.0
C3.6667
D1.0
💡 Hint
Check the 'Calculate bin edges' row in execution_table, column 'Result'
At which step do we get the counts of data points in each bin?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Count data points in bins' action in execution_table
If we increase bins from 3 to 4, how does the counts array length change?
AIt becomes length 5
BIt becomes length 4
CIt stays the same length
DIt becomes length 3
💡 Hint
Counts array length equals number of bins (see variable_tracker for counts)
Concept Snapshot
np.histogram(data, bins)
- Returns counts and bin edges
- counts length = bins
- edges length = bins + 1
- bins divide data range evenly by default
- Left edge inclusive, right edge exclusive except last bin
Full Transcript
We start with a data array and decide how many bins to use. Then we calculate the bin edges that split the data range into equal parts. Next, we count how many data points fall into each bin. Finally, we output the counts and edges. For example, with data [1,2,2,3,4,5] and 3 bins, edges are [1.0, 2.3333, 3.6667, 5.0] and counts are [3,1,2]. There are always one more edges than bins. Bins include the left edge but exclude the right edge except the last bin includes both edges.