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)] |
我想绘制时间序列,重点放在总体趋势上,而不是在小浪上。有没有办法绘制一段时间内的平均值,并用表示波浪的条纹包围(条纹应代表置信区间,此时数据点可能在该区间)?
一个简单的情节看起来像这样:
我想要的带有置信区间的图看起来像这样:
在Python中有一种优雅的方法吗?
您可以使用
对于置信区间的阴影(用标准偏差之间的间隔表示),可以使用
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. |
使用上面的代码,您将获得如下内容:
看起来,您将标准值加倍了两次。我想应该是这样的:
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) |