Compute the histogram of nums against the bins using NumPy


In python, for creating histograms we have the numpy, matplotlib and seaborn libraries. In Numpy, we have the function namely histogram() to work with the histogram data. The input argument for the histogram() function is the nums and bins. The nums are used to create the numerical data.

Before proceeding with the examples first of all let us understand what is histogram.

What is histogram

Histogram is the graphical representation of the dataset distribution. It represents the data in the form of series of bars, where the range of data values represented by each bar and height of the bar represents the frequency of the data values defined within the range.

These are mainly used to represent the distribution of the numerical data like grades in a class, distribution of the population or distribution of the incomes of the employees etc.

Calculating histogram using Numpy in Python

In histogram, x-axis represents the range of data values, divided into intervals and the y-axis represents the frequency of the range of data values within the each bin. The histograms can be normalized by dividing the frequency of each bin by the total data values, which results to the relative frequency histogram where the y-axis represents the data values of each bin.

Syntax

Following is the syntax for creating the histograms for the given range of data with the nums.

numpy.histogram(nums, bins, range, normed, weights, density)

Where,

  • nums is the input numerical data.

  • bins are the number of bars to be in the graph to represent the data.

  • range defines the range of values to be in the histogram.

  • normed is in favor of the density parameter.

  • weights are the optional parameter which weights for each data value.

  • Density is the parameter to normalize the histogram data to form probability density.

Example

In the following example, we are creating the score of the students by defining the nums and how many bins are needed to be in the histogram. The histogram() function is used to generate the histogram by passing the nums and bins as the input arguments.

import numpy as np
import matplotlib.pyplot as plt
nums = np.random.normal(50,20,size = 50)
hist = np.histogram(nums)
print("The histogram of the given data:",hist)
plt.hist(hist)
plt.show()

Output

Following is the output of the histogram() function using the nums against the bins.

The histogram of the given data: (array([ 1,  0,  0,  6,  7,  9,  8, 12,  4,  3]), array([-11.52097959,  -1.64606252,   8.22885455,  18.10377162,
        27.97868869,  37.85360576,  47.72852282,  57.60343989,
        67.47835696,  77.35327403,  87.2281911 ]))

Example

Let’s see another example to understand the working of the histogram() using the nums against bins.

import numpy as np
import matplotlib.pyplot as plt
nums = np.random.normal(200,20,size = 100)
hist = np.histogram(nums)
print("The histogram of the given data:",hist)
plt.hist(hist)
plt.show()

Output

Following is the output of the histogram() function using the nums against the bins.

The histogram of the given data: (array([ 2,  1,  8, 17, 18, 24, 13, 11,  3,  3]), array([146.40363927, 156.62124167, 166.83884407, 177.05644647,
       187.27404887, 197.49165127, 207.70925367, 217.92685607,
       228.14445847, 238.36206086, 248.57966326]))

Example

Let’s see another example to work with histogram() function of the numpy library to use the nums against the bins.

import numpy as np
import matplotlib.pyplot as plt
nums = np.random.normal(400,30,size = 30)
hist = np.histogram(nums)
print("The histogram of the given data:",hist)
plt.hist(hist)
plt.show()

Output

Following is the output of the histogram() function using the nums against the bins.

The histogram of the given data: (array([2, 1, 3, 5, 5, 4, 6, 0, 2, 2]), array([340.28832063, 352.48676341, 364.68520618, 376.88364896,
       389.08209174, 401.28053451, 413.47897729, 425.67742007,
       437.87586284, 450.07430562, 462.2727484 ]))

Updated on: 07-Aug-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements