Compute the histogram of a set of data in Python

In the tutorial, you will learn how to compute the histogram of a set of data. By using the NumPy module to show the data in the histogram, from picture view using the matplotlib module that will compute the histogram of a set of data.

For a better knowledge view of the huge data set, the histograms will be helpful.

Uses of Histogram:

  • To see any variations in the consider data set.
  • To easily see the distribution of data.
  • The large data values will be relatively in simple chart form.

NumPy.histogram():

The NumPy module considers a lot of inbuilt functions one of it’s will be histograms consider two values bins and data set.

Let’s have a look at the general attributes of an array. They are as follows.

  • An array can hold many values based on a single name.
  • Access the elements based on the index number.
  • We can slice the elements in the array [start: end] based on the start and end position -1 elements display the results.

Importing module:

import matplotlib.pyplot as p
import numpy

From the above to modules NumPy will be used to the histogram representation and matplotlib will be used to picture view of data set.

Program to display the histogram:

From the below code you get an idea about the histogram of a data set.

import matplotlib.pyplot as p
import numpy
x=[1,2,3,999]
print(numpy.histogram(x))
p.hist(x)
p.show()

Output:

(array([3, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=int32), array([  1. , 100.8, 200.6, 300.4, 400.2, 500. , 599.8, 699.6, 799.4,
       899.2, 999. ]))

The histogram image will be as below:
Compute the histogram of a set of data in Python

Explanation:
  • From the above code, we consider a data set x consider three values [1,2,3].
  • By using the NumPy module histogram function display the output.
  • For the picture view, we consider the Mathplotlib module displays the picture view of the histogram by hist() function.

Also read: Plot a histogram for an Image in PIL in Python and Plotting a Histogram in Python using Seaborn

Leave a Reply