Open In App

Matplotlib.axes.Axes.hist() in Python

Last Updated : 13 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.hist() Function

The Axes.hist() function in axes module of matplotlib library is used to plot a histogram.
Syntax: Axes.hist(self, x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, 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.axes.Axes.hexbin() function in matplotlib.axes: Example-1: Python3
# 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
fig, ax = plt.subplots()

n, bins, patches = ax.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))
ax.plot(bins, y, '--', color ='black')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')

ax.set_title('matplotlib.axes.Axes.hist() Example')
plt.show()
Output: Example-2: Python3
# 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)

fig, [(ax0, ax1), (ax2, ax3)] = plt.subplots(nrows = 2,
                                             ncols = 2)


colors = ['green', 'blue', 'lime']

ax0.hist(x, n_bins, density = True, 
         histtype ='bar',
         color = colors, 
         label = colors)

ax0.legend(prop ={'size': 10})

ax1.hist(x, n_bins, density = True,
         histtype ='barstacked',
         stacked = True, 
         color = colors)

ax2.hist(x, n_bins, histtype ='step',
         stacked = True,
         fill = False, 
         color = colors)

x_multi = [np.random.randn(n) for n in [100000,
                                        80000,
                                        1000]]

ax3.hist(x_multi, n_bins, 
         histtype ='stepfilled',
         color = colors)

plt.show()
Output:

Next Article
Article Tags :
Practice Tags :

Similar Reads