📜  Python中的 Matplotlib.axes.Axes.hist()

📅  最后修改于: 2022-05-13 01:55:39.387000             🧑  作者: Mango

Python中的 Matplotlib.axes.Axes.hist()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。

matplotlib.axes.Axes.hist()函数

matplotlib 库的 axes 模块中的Axes.hist()函数用于绘制直方图。

下面的示例说明了 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()

输出: