Python numpy.histogram() method with example

In this tutorial, we will learn how to use the histogram function of NumPy library in Python. A histogram is a graphical representation of a dataset by splitting it into equal-sized intervals called bins. This Numpy histogram function gives the numerical representation of the dataset. To get the graphical representation, we need to use hist function of matplotlib library.

Using NumPy.histogram()

The function histogram() is a built-in function of NumPy library. It takes a dataset in the form of an array and the number of bins that need to be formed. The output is also in the form of an array that represents the values.


numpy.histogram(data, bins=10, range=None, normed=None, weights=None, density=None)


  • data: dataset in the form of an array
  • bins: optional parameter may be int or sequence of str defining number of bins
  • range: optional parameter gives a lower and upper range of bins
  • normed: optional parameter same as density attribute but produces incorrect output in some cases, don’t use it
  • weights: optional parameter having an array of weights with the same shape as data
  • density: optional parameter if True the result is the value of the probability function at the bin because it will be normalized such that the integral over the range is 1 and if False the result will contain the number of samples in each bin. It overrides the normed keyword if present.


  • hist: an array of values for the histogram
  • bin_edges: an array of bin edges


import numpy as np
x = np.random.randint(100, size =(25))
np.histogram(x,bins = [0,20,40,60,80,100]) 
hist, binedge = np.histogram(x, bins = [0,20,40,60,80,100])
print (hist)
print (binedge)


[ 4 5 3 3 10]
[ 0 20 40 60 80 100]

You may also learn,

Graphical Representation

The above numeric representation can be converted into a graphical form of histogram. To do so, we can use hist() 0f pyplot submodule present in the matplotlib library. The parameters of this function are dataset and bins and the output will be a histogram.  The graph contains rectangles with the same horizontal size as bin size and height as the frequency.


from matplotlib import pyplot as plt
import numpy as np 
a = np.random.randint(100, size =(25))
fig = plt.figure(figsize =(10, 7))
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title("Numpy Histogram")



Leave a Reply

Your email address will not be published. Required fields are marked *