np.histogram()
A histogram counts how many values fall into each range (bin) without drawing anything.
In this page:
Syntax
counts, bin_edges = np.histogram(arr, bins=number_of_bins)
np.histogram()
np.histogram(a, bins) returns two arrays: counts per bin and the bin edges, which is one longer than the counts. bins can be an integer or explicit edges. It provides the numbers behind a histogram plot.
Note:
Set density=True to get probabilities instead of counts.
Example: np.histogram()
import numpy as np
a = np.array([1, 2, 2, 3, 3, 3, 4, 4, 5, 9])
counts, edges = np.histogram(a, bins=[0, 3, 6, 10])
print(counts)
print(edges)
# Output:
# [3 6 1]
# [ 0 3 6 10]
Related Topics
Common Mistakes
- Forgetting edges has one more item than counts
- Expecting a plot to appear
- Using too few or too many bins
Chapter Summary
- Returns counts and edges
- Edges are one longer than counts
- bins can be a number or list
- density normalizes
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: