Matplotlib Histrograms – hist() Function

What is Histogram?

Before diving into how to create histograms in matplotlib, let us first understand what is a histogram?

So a histogram is an accurate representation of the distribution of numerical data.

  • So Histogram is a type of bar graph and it was invented by Karl Pearson
  • The Histogram is mainly used to represent the data that is provided in some groups.
  • Histograms usually consist of bins of data(consecutive and non-overlapping intervals of variables), where each bin consists of minimum and maximum values.
  • To estimate the probability distribution of the continuous variable, histogram is used.

Creating a Histogram

There are a few steps that should be kept in mind while creating a Histogram:

  1. The first step is to create the bin of the ranges.
  2. The second step is to distribute the whole range of the values into a corresponding series of intervals.
  3. The third step is to count the values in each interval.

matplotlib.pyplot.hist() Function

This function is used to create the histogram.

Let us discuss the parameters of the histogram and the detailed description is given below:

  • xThis parameter indicates an array or sequence of arrays.
  • binsThis parameter indicates an integer or sequences or any string.
  • densityThis is an optional parameter that consists of boolean values.
  • rangeThis is an optional parameter used to indicate upper and lower range of bins and it is also an optional parameter.
  • labelThis is an optional parameter and is used to set the histogram axis on a log scale.
  • colorThis is an optional parameter used to set the color.
  • cumulativeIf the value of this option is set to true, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values.
  • histtypeThis is an optional parameter used to specify the type of histogram [that is barbarstackedstepstepfilled]. The default value of this parameter is “bar“.
  • alignThis is an optional parameter that controls the plotting of histogram having values [leftrightmid].

Let us take a look at a few examples to understand the concept of the histogram.

Simple Histogram Plot Example:

Below we have a simple example to create a histogram:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
n, bins, patches = plt.hist(x, num_bins, facecolor='orange', alpha=0.8)

The output in the form of histogram is as follows:

simple histogram example matplotlib

Two Histogram in one Figure Example:

Let us try two plot with two histograms together. In the code snippet given below, we are trying to draw two histograms together:

2D Histogram Example:

Let us try to create a two-dimensional histogram. The code snippet for the 2D histogram is as follows:

import numpy as np
import matplotlib.pyplot as plt
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T # x and y are array that are drawn from a multivariate Gaussian distribution

plt.hist2d(x, y, bins=30, cmap='CMRmap') #plt.hist2d is used to draw histogram for 2D
cb = plt.colorbar()
cb.set_label('counts in bin')

Output Histogram is as follows:

2D histogram example matplotlib
Follow Us On