简单的指数平滑预测不是在Python中的实际数据之上绘图

问题描述 投票:0回答:1

我正在使用statsmodels包中的SimpleExpSmoothing。我创建了一个时间序列数据帧。我能够为预测和实际数据创建图。但是当我试图将它们一起绘制时,它们显示为单独的图。我想在实际数据之上绘制预测图。

我使用的是statsmodels 0.9.0,python 3.6.8和matplotlib 3.0.2

# Simple Exponential Smoothing
fit1 = SimpleExpSmoothing(df1).fit(smoothing_level=0.2,optimized=False)
fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$')
# plot
fcast1.plot(marker='o', color='blue', legend=True)
fit1.fittedvalues.plot(marker='o',  color='blue')



fit2 = SimpleExpSmoothing(df1).fit(smoothing_level=0.6,optimized=False)
fcast2 = fit2.forecast(12).rename(r'$\alpha=0.6$')
# plot
fcast2.plot(marker='o', color='red', legend=True)
fit2.fittedvalues.plot(marker='o', color='red')


fit3 = SimpleExpSmoothing(df1).fit()
fcast3 = fit3.forecast(12).rename(r'$\alpha=%s$'%fit3.model.params['smoothing_level'])
# plot
fcast3.plot(marker='o', color='green', legend=True)
fit3.fittedvalues.plot(marker='o', color='green')

df1.plot()

plt.show()

我有两个图,一个包含所有预测图,另一个图包含实际数据。我想在实际情况的基础上预测一个情节。

python matplotlib statsmodels
1个回答
0
投票

这与Statsmodels没有关系,这只是Pandas绘图的工作方式。确保它们都在同一图中绘制的一种方法是自己构造图形,然后明确地传递轴:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

# ...

fcast1.plot(ax=ax, marker='o', color='blue', legend=True)
fit1.fittedvalues.plot(ax=ax, marker='o',  color='blue')

# etc.

df1.plot(ax=ax)
© www.soinside.com 2019 - 2024. All rights reserved.