使用柱状图绘制累积分布

这说明了如何绘制一个累积的、标准化的直方图作为一个阶跃函数,以便可视化一个样本的经验累积分布函数(CDF)。我们还展示了理论CDF。

其他一些选择 hist 功能已演示。也就是说,我们使用 normed 用于规范化柱状图的参数和 cumulative normed 参数采用布尔值。什么时候? True ,对垃圾箱高度进行缩放,使柱状图的总面积为1。这个 cumulative Kwarg更微妙了。喜欢 normed ,您可以传递它true或false,但也可以传递它-1来反转分布。

因为我们显示的是一个标准化和累积直方图,所以这些曲线实际上是样本的累积分布函数(CDF)。在工程中,经验CDF有时被称为“非超越”曲线。换句话说,您可以查看给定x值的y值,以获得不超过x值的样本的概率和观察值。例如,X轴上的225值对应于Y轴上的约0.85,因此样本中的观测值不超过225的概率为85%。相反,设置, cumulative 在本例的最后一个系列中,为-1创建一条“超越”曲线。

选择不同的仓位计数和大小会显著影响直方图的形状。Astropy文档有一个关于如何选择这些参数的重要部分:http://docs.astropy.org/en/stable/visualization/histogram.html

Cumulative step histograms
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)

mu = 200
sigma = 25
n_bins = 50
x = np.random.normal(mu, sigma, size=100)

fig, ax = plt.subplots(figsize=(8, 4))

# plot the cumulative histogram
n, bins, patches = ax.hist(x, n_bins, density=True, histtype='step',
                           cumulative=True, label='Empirical')

# Add a line showing the expected distribution.
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
y = y.cumsum()
y /= y[-1]

ax.plot(bins, y, 'k--', linewidth=1.5, label='Theoretical')

# Overlay a reversed cumulative histogram.
ax.hist(x, bins=bins, density=True, histtype='step', cumulative=-1,
        label='Reversed emp.')

# tidy up the figure
ax.grid(True)
ax.legend(loc='right')
ax.set_title('Cumulative step histograms')
ax.set_xlabel('Annual rainfall (mm)')
ax.set_ylabel('Likelihood of occurrence')

plt.show()

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery