Python中的 Matplotlib.pyplot.hist()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
matplotlib.pyplot.hist()函数
matplotlib 库的 pyplot 模块中的hist()函数用于绘制直方图。
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.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.hist()函数:
示例 #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()
输出:
示例 #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()
输出: