I'm trying to put together a few stats about the history of my data to better understand it. My goal is to create a loop which creates a pdf for trend. seasonality and resid for each column in my data frame. I wrote a loop to do this, but each graph includes more data than it should. For example, the first graph created has only 1 line, but then the next graph created has the line from the first graph and a new line.
I created some fake data for testing purposes.
What I have tried:
#Creating random Data
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import statsmodels.api as sm
#creating random data
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
data2 = np.random.randint(1, high=200, size=len(days))
df = pd.DataFrame({'test': days, 'col1': data, 'col2': data2})
df = df.set_index('test')
print(df)
#Loop to create a resid, trend, and seasonality plot for each column in the dataframe
for i in df:
decomposition = sm.tsa.seasonal_decompose(df[i], model = 'additive')
decomposition.resid.plot()
plt.savefig('{} resid.pdf'.format(i), bbox_inches='tight')
decomposition.seasonal.plot()
plt.savefig('{} seasonal.pdf'.format(i), bbox_inches='tight')
decomposition.trend.plot()
plt.savefig('{} trend.pdf'.format(i), bbox_inches='tight')