0

I'm trying to make two subplots on the same figure with several sets of time series. In the representation below, I would like that the upper sub-plot has only the series called "original", and the rest below. However, my code groups them all in the subplot below. You can see how it looks now:

enter image description here

The implemented code is the following one:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
# Data loading

data = pd.read_csv('setpoints.csv', parse_dates=['date'], index_col=['date'])

df_data = pd.DataFrame(data, columns=['Data'])

df_matric['Data'].index.freq = 'MS'
train, test = df_data['Data'], df_data['Data']
model = ExponentialSmoothing(train, trend='mul', seasonal='add', seasonal_periods=12).fit()
period = ['Jan-12', 'Dec-14']
pred = model.predict(start=period[0], end=period[1])
print(pred)

df_data['Data'].plot(label='Train')
test.plot(label='Test')
pred.plot(label='Holt-Winters')
plt.legend(loc='best')
plt.show()

fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Submissions data')
ax1.set_ylim(0, 300000)
ax1.set_ylabel('Submissions')
ax1 = df_data['Data'].plot(label='Original', color = 'blue')
ax1.legend()
ax2 = df_data['Data'].plot(label='Train')
ax2 = test.plot(label='Test')
ax2 = pred.plot(label='Holt-Winters')
ax2.set_xlabel('Date')
ax2.set_ylabel('Submissions')
ax2.legend()
plt.show()

And the data set is the following one:

date,Data

Jan-12,153046

Feb-12,161874

Mar-12,226134

Apr-12,171871

May-12,191416

Jun-12,230926

Jul-12,147518

Aug-12,107449

Sep-12,170645

Oct-12,176492

Nov-12,180005

Dec-12,193372

Jan-13,156846

Feb-13,168893

Mar-13,231103

Apr-13,187390

May-13,191702

Jun-13,252216

Jul-13,175392

Aug-13,150390

Sep-13,148750

Oct-13,173798

Nov-13,171611

Dec-13,165390

Jan-14,155079

Feb-14,172438

Mar-14,225818

Apr-14,188195

May-14,193948

Jun-14,230964

Jul-14,172225

Aug-14,129257

Sep-14,173443

Oct-14,188987

Nov-14,172731

Dec-14,211194

Does anyone now how can I solve it? Or a better way to do it

srgam
  • 366
  • 1
  • 13
  • can you share the whole code ? where is `df_matric` and `ExponentialSmoothing` ? – ZINE Mahmoud May 30 '20 at 12:11
  • It is really `df_data`, and `ExponentialSmoothing` is a function imported by statsmodels. (I updated the code now) You can consider 4 different time series sets if it's easier to see it – srgam May 30 '20 at 12:17
  • 1
    Does this answer your question? [How can I plot separate Pandas DataFrames as subplots?](https://stackoverflow.com/questions/22483588/how-can-i-plot-separate-pandas-dataframes-as-subplots) – Diziet Asahi May 30 '20 at 12:27

1 Answers1

0

I believe the problem with your code is that you are calling plt.show() twice in your python session. I would suggest you call it once at the end of your script. I think it will solve your problem.

The way plt.show() work is that it looks for all the currently active figure objects, and opens interactive windows that display your results. Calling it multiple times one session can lead to unpredictable behaviour and it mostly should be avoided.