Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 =
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')

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')

#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')
    plt.savefig('{} resid.pdf'.format(i), bbox_inches='tight')

    plt.savefig('{} seasonal.pdf'.format(i), bbox_inches='tight')

    plt.savefig('{} trend.pdf'.format(i), bbox_inches='tight')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900