Skip to content
Related Articles

Related Articles

Matplotlib.pyplot.hist() in Python

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 21 Apr, 2020

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.hist() Function

The hist() function in pyplot module of matplotlib library is used to plot a histogram.

Syntax: matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\*kwargs)

Parameters: This method accept the following parameters that are described below:

  • x : This parameter are the sequence of data.
  • bins : This parameter is an optional parameter and it contains the integer or sequence or string.
  • range : This parameter is an optional parameter and it the lower and upper range of the bins.
  • density : This parameter is an optional parameter and it contains the boolean values.
  • weights : This parameter is an optional parameter and it is an array of weights, of the same shape as x.
  • bottom : This parameter is the location of the bottom baseline of each bin.
  • histtype : This parameter is an optional parameter and it is used to draw type of histogram. {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}
  • align : This parameter is an optional parameter and it controls how the histogram is plotted. {‘left’, ‘mid’, ‘right’}
  • rwidth : This parameter is an optional parameter and it is a relative width of the bars as a fraction of the bin width
  • log : This parameter is an optional parameter and it is used to set histogram axis to a log scale
  • color : This parameter is an optional parameter and it is a color spec or sequence of color specs, one per dataset.
  • label : This parameter is an optional parameter and it is a string, or sequence of strings to match multiple datasets.
  • normed : This parameter is an optional parameter and it contains the boolean values.It uses the density keyword argument instead.

Returns: This returns the following:

  • n :This returns the values of the histogram bins.
  • bins :This returns the edges of the bins.
  • patches :This returns the list of individual patches used to create the histogram.

Below examples illustrate the matplotlib.pyplot.hist() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
   
np.random.seed(10**7)
mu = 121 
sigma = 21
x = mu + sigma * np.random.randn(1000)
   
num_bins = 100
   
n, bins, patches = plt.hist(x, num_bins, 
                            density = 1
                            color ='green',
                            alpha = 0.7)
   
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
  
plt.plot(bins, y, '--', color ='black')
  
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
  
plt.title('matplotlib.pyplot.hist() function Example\n\n',
          fontweight ="bold")
  
plt.show()

Output:

Example #2:




# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
    
np.random.seed(10**7)
n_bins = 20
x = np.random.randn(10000, 3)
    
colors = ['green', 'blue', 'lime']
  
plt.hist(x, n_bins, density = True
         histtype ='bar',
         color = colors,
         label = colors)
  
plt.legend(prop ={'size': 10})
  
plt.title('matplotlib.pyplot.hist() function Example\n\n',
          fontweight ="bold")
  
plt.show()

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!