如何在 Python 中绘制置信区间? [英] How to plot confidence interval in Python?

查看:58
本文介绍了如何在 Python 中绘制置信区间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Python,但我无法理解如何绘制给定数据(或一组数据)的置信区间.我已经有一个函数,根据我传递给它的置信水平,给定一组测量值,计算上限和下限,但我不知道如何使用这两个值来绘制置信区间.我知道这里已经有人问过这个问题,但我觉得答案没有用.

解决方案

有几种方法可以完成您的要求:

仅使用 matplotlib

from matplotlib import pyplot as plt将 numpy 导入为 np#一些示例数据x= np.linspace(0.1, 9.9, 20)y = 3.0 * x#一些置信区间ci = 1.96 * np.std(y)/np.mean(y)图, ax = plt.subplots()ax.plot(x,y)ax.fill_between(x, (y-ci), (y+ci), color='b', alpha=.1)

fill_between 可以满足您的需求.有关如何使用此函数的更多信息,请参阅:

或者,选择 seaborn,它支持使用 lineplotregplot,参见:https://seaborn.pydata.org/generated/seaborn.lineplot.html

I recently started to use Python and I can't understand how to plot a confidence interval for a given datum (or set of data). I already have a function that computes, given a set of measurements, a higher and lower bound depending on the confidence level that I pass to it, but I don't know how to use those two values to plot a confidence interval. I know that this question have been already asked here but I did not find the answers useful.

There are several ways to accomplish what you asking for:

Using only 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.mean(y)

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

fill_between does what you are looking for. For more information on how to use this function, see: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.fill_between.html

Output

Alternatively, go for seaborn, which supports this using lineplot or regplot, see: https://seaborn.pydata.org/generated/seaborn.lineplot.html

这篇关于如何在 Python 中绘制置信区间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆