简体   繁体   English

如何将Python中的时间序列数据的plot置信区间?

[英]How to plot confidence interval of a time series data in Python?

There are multiple questions exist on this area, however I can not use them to solve my question.这个领域存在多个问题,但我不能用它们来解决我的问题。 I have a data sample and I want to create the confidence interval for its curve.我有一个数据样本,我想为其曲线创建置信区间。 Here, I provide a simple example:在这里,我提供一个简单的例子:

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt

mean, lower, upper = [],[],[]
ci = 0.2
for i in range (20):

    a = np.random.rand(100) # this is the output

    MEAN = np.mean(a)
    mean.append(MEAN)
    std = np.std(a)
    Upper = MEAN+ci*std
    Lower = MEAN-ci*std

    lower.append(Lower)
    upper.append(Upper)


 plt.figure(figsize=(20,8))
 plt.plot(mean,'-b', label='mean')
 plt.plot(upper,'-r', label='upper')
 plt.plot(lower,'-g', label='lower')

 plt.xlabel("Value",   fontsize = 30)
 plt.ylabel("Loss", fontsize = 30)
 plt.xticks(fontsize= 30) 
 plt.yticks(fontsize= 30) 
 plt.legend(loc=4, prop={'size': 30})

In the above example, I drew %80 confidence interval.在上面的示例中,我绘制了 %80 置信区间。 I have two questions:我有两个问题:

1- Could you please tell me that this way of calculating and plotting the confidence interval is true? 1- 你能告诉我这种计算和绘制置信区间的方法是正确的吗?

2- I want to color the shadow area of the confidence interval. 2-我想给置信区间的阴影区域上色。 I have attached a figure, I want some thing like that.我附上了一个数字,我想要这样的东西。 Could you please tell me if you have any solution?你能告诉我你有什么解决办法吗? Thanks for your help.谢谢你的帮助。

在此处输入图像描述

I'm not qualified to answer question 1, however the answers to this SO question produce different results from your code.我没有资格回答问题 1,但是这个SO 问题的答案与您的代码产生不同的结果。

As for question 2, you can use matplotlib fill_between to fill the area between two curves (the upper and lower of your example).至于问题2,您可以使用 matplotlib fill_between来填充两条曲线之间的区域(您的示例的上部和下部)。

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats

# https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data
def mean_confidence_interval(data, confidence=0.95):
    a = 1.0 * np.array(data)
    n = len(a)
    m, se = np.mean(a), scipy.stats.sem(a)
    h = se * scipy.stats.t.ppf((1 + confidence) / 2., n-1)
    return m, m-h, m+h

mean, lower, upper = [],[],[]
ci = 0.8
for i in range (20):
    a = np.random.rand(100) # this is the output
    m, ml, mu = mean_confidence_interval(a, ci)
    mean.append(m)
    lower.append(ml)
    upper.append(mu)

plt.figure()
plt.plot(mean,'-b', label='mean')
plt.plot(upper,'-r', label='upper')
plt.plot(lower,'-g', label='lower')
# fill the area with black color, opacity 0.15
plt.fill_between(list(range(len(mean))), upper, lower, color="k", alpha=0.15)

plt.xlabel("Value")
plt.ylabel("Loss")
plt.legend()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM