源代码:
#导出matplotlib.pyplot,matplotlib.mlab 重命名为 plt,mlab
#导出numpy包重命名为np
import numpy as np
import matplotlib.mlab as mlab
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, normed=1)
# 添加一个“最适合”行
y = mlab.normpdf(bins, mu, sigma)
ax.plot(bins, y, '--')
ax.set_xlabel('smarts')
ax.set_ylabel('probability density')
ax.set_title(r'histogram of iq: $mu=100$, $sigma=15$')
# 调整间距,以防止ylabel剪辑
fig.tight_layout()
plt.show()