Python中的 Matplotlib.axes.Axes.hist()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.hist()函数
matplotlib 库的 axes 模块中的Axes.hist()函数用于绘制直方图。
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.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.hexbin()函数:
示例 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
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()
输出:
示例 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)
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()
输出: