Matplotlib Histrograms - hist() Function
In this tutorial, we will cover how to create histogram plots in Python using matplotlib library.
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:
-
The first step is to create the bin of the ranges.
-
The second step is to distribute the whole range of the values into a corresponding series of intervals.
-
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:
-
x
This parameter indicates an array or sequence of arrays.
-
bins
This parameter indicates an integer or sequences or any string.
-
density
This is an optional parameter that consists of boolean values.
-
range
This is an optional parameter used to indicate upper and lower range of bins and it is also an optional parameter.
-
label
This is an optional parameter and is used to set the histogram axis on a log scale.
-
color
This is an optional parameter used to set the color.
-
cumulative
If 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.
-
histtype
This is an optional parameter used to specify the type of histogram [that is bar, barstacked, step, stepfilled]. The default value of this parameter is "bar".
-
align
This is an optional parameter that controls the plotting of histogram having values [left, right, mid].
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)
plt.show()
The output in the form of histogram is as follows:
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')
plt.show()
Output Histogram is as follows: