受到这个
question的启发,你如何在python中制作同样的情节?该图旨在对您的分布如何偏离预期分布进行良好的直观表示.它会将直方图的条形挂在预期的分布线上,因此在条形图的底部和x轴之间读取与预期值的差异,而不是在条形图的顶部和预期的分布曲线之间.
我找不到任何内置功能.
最佳答案 我们的想法是只移动直方图的每个条形,其中条形顶部处于预期值:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
fig, ax = plt.subplots(1, 2)
mu = 10
sig = 0.3
my_data = np.random.normal(mu, sig, 200)
x = np.linspace(9, 11, 100)
# I plot the data twice, one for the histogram only for comparison,
# and one for the rootogram.
# The trick will be to modify the histogram to make it hang to
# the expected distribution curve:
for a in ax:
a.hist(my_data, normed=True)
a.plot(x, mlab.normpdf(x, mu, sig))
a.set_ylim(-0.2)
a.set_xlim(9, 11)
a.hlines(0, 9, 11, linestyle="--")
for rectangle in ax[1].patches:
# expected value in the middle of the bar
exp = mlab.normpdf(rectangle.get_x() + rectangle.get_width()/2., mu, sig)
# difference to the expected value
diff = exp - rectangle.get_height()
rectangle.set_y(diff)
ax[1].plot(rectangle.get_x() + rectangle.get_width()/2., exp, "ro")
ax[0].set_title("histogram")
ax[1].set_title("hanging rootogram")
plt.tight_layout()
这使:
HTH