直方图(hist)函数的几个特性演示
除基本直方图外,此演示还显示了一些可选功能:
- 设置数据箱的数量。
标准化标志,用于标准化箱高度,使直方图的积分为1.得到的直方图是概率密度函数的近似值。
- 设置条形的面部颜色。
- 设置不透明度(alpha值)。
选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import matplotlib import numpy as np import matplotlib.pyplot as plt
np.random.seed(19680801)
mu = 100 sigma = 15 x = mu + sigma * np.random.randn(437)
num_bins = 50
fig, ax = plt.subplots()
n, bins, patches = ax.hist(x, num_bins, density=1)
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * (1 / sigma * (bins - mu))**2)) ax.plot(bins, y, '--') ax.set_xlabel('Smarts') ax.set_ylabel('Probability density') ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
fig.tight_layout() plt.show()
|

参考
此示例显示了以下函数和方法的使用:
1 2 3 4
| matplotlib.axes.Axes.hist matplotlib.axes.Axes.set_title matplotlib.axes.Axes.set_xlabel matplotlib.axes.Axes.set_ylabel
|
下载这个示例