python - 如何在 Python 中绘制置信区间?

标签 python python-3.x confidence-interval

我最近开始使用 Python,但我不明白如何绘制给定数据(或数据集)的置信区间。

我已经有一个函数,可以在给定一组测量值的情况下根据传递给它的置信水平来计算上限和下限,但是如何使用这两个值来绘制置信区间?

最佳答案

有几种方法可以实现您的要求:

仅使用matplotlib

from matplotlib import pyplot as plt
import numpy as np

#some example data
x = np.linspace(0.1, 9.9, 20)
y = 3.0 * x
#some confidence interval
ci = 1.96 * np.std(y)/np.sqrt(len(x))

fig, ax = plt.subplots()
ax.plot(x,y)
ax.fill_between(x, (y-ci), (y+ci), color='b', alpha=.1)

fill_ Between 可以满足您的要求。有关如何使用此功能的更多信息,请参见:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.fill_between.html

输出

enter image description here

或者,选择 seaborn,它使用 lineplotregplot 支持此操作, 请参阅:https://seaborn.pydata.org/generated/seaborn.lineplot.html

关于python - 如何在 Python 中绘制置信区间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59747313/

相关文章:

python - 在 Python 中将时间增量相加

python - 变量没有传递到较低级别?

python - 如何将图像插入按钮?

python - Seaborn tsplot 不显示 CI strip

python - 处理try-except block 内的错误后,Python处理错误

python - pandas 不同列中唯一值的频率

python - 从多维 FFT 中提取频率

Python被多个分隔符分割,包括空格?

R PCA 图使用 Hotelling 的 T2 作为置信区间

r - 如何使用预引导数据获取 BCa CI?