gpt4 book ai didi

python - 如何在 python 中绘制悬挂根图?

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:11 25 4
gpt4 key购买 nike

受此启发question ,你如何在python中制作相同类型的情节?该图旨在以直观的方式展示您的分布如何偏离预期分布。它将直方图的条形卡在预期分布线上,因此与预期值的差异是在条形底部和 x 轴之间读取的,而不是在条形顶部和预期分布曲线之间读取的。

我找不到任何内置函数。

hanging rootogram

最佳答案

想法是移动直方图的每个条形图,使条形图的顶部位于预期值处:

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()

给出:

Hanging rootogram python

HTH

关于python - 如何在 python 中绘制悬挂根图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com