← Back to NumPy Course | Chapter 8: Statistics | Lesson 4 of 6

np.histogram()

A histogram counts how many values fall into each range (bin) without drawing anything.

In this page:

  1. np.histogram()
Syntax
python
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()

python
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
  1. Forgetting edges has one more item than counts
  2. Expecting a plot to appear
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.