关于matplotlib:如何在python中绘制显示置信区间的时间序列数组?

How to plot a time series array, with confidence intervals displayed, in python?

我有一个逐渐增加的时间序列,但是在很短的时间内,它们非常波动。例如,时间序列可能如下所示:

1
[10 + np.random.rand() for i in range(100)] + [12 + np.random.rand() for i in range(100)] + [14 + np.random.rand() for i in range(100)]

我想绘制时间序列,重点放在总体趋势上,而不是在小浪上。有没有办法绘制一段时间内的平均值,并用表示波浪的条纹包围(条纹应代表置信区间,此时数据点可能在该区间)?

一个简单的情节看起来像这样:

enter

对于置信区间的阴影(用标准偏差之间的间隔表示),可以使用matplotlib.pyplot中的函数fill_between()。有关更多信息,您可以在这里看看,从中启发了以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import numpy             as np
import pandas            as pd
import matplotlib.pyplot as plt

#Declare the array containing the series you want to plot.
#For example:
time_series_array = np.sin(np.linspace(-np.pi, np.pi, 400)) + np.random.rand((400))
n_steps           = 15 #number of rolling steps for the mean/std.

#Compute curves of interest:
time_series_df = pd.DataFrame(time_series_array)
smooth_path    = time_series_df.rolling(n_steps).mean()
path_deviation = 2 * time_series_df.rolling(n_steps).std()

under_line     = (smooth_path-path_deviation)[0]
over_line      = (smooth_path+path_deviation)[0]

#Plotting:
plt.plot(smooth_path, linewidth=2) #mean curve.
plt.fill_between(path_deviation.index, under_line, over_line, color='b', alpha=.1) #std curves.

使用上面的代码,您将获得如下内容:
enter

1
2
3
4
5
time_series_df = pd.DataFrame(time_series_array)
smooth_path = time_series_df.rolling(20).mean()
path_deviation = time_series_df.rolling(20).std()
plt.plot(smooth_path, linewidth=2)
plt.fill_between(path_deviation.index, (smooth_path-2*path_deviation)[0], (smooth_path+2*path_deviation)[0], color='b', alpha=.1)