Trexion Kameha
Trexion Kameha

Reputation: 3580

Python: Creating one chart from same position in multiple dataframes

I have 3 dataframes, each with columns A, B, C, and D. One dataframe contains the values, the second contains a rolling mean, and the third contains the rolling standard deviation:

#data
df = pd.DataFrame(np.random.randint(0,100,size=(1000, 4)), columns=list('ABCD'))
#rolling mean
df_mu = df.rolling(window=100).mean()
#rolling standard deviation
df_sd = df.rolling(window=100).std()

I would like to create one chart that has 3 series: A, A's rolling mean, A's rolling STD. Essentially, in one chart, I want to see df.A, df_mu.A, df_sd.A

I would like to then repeat the process for B, C, and D.

The result is 4 charts, with 3 series in each chart. I would like these charts to be displayed within the iPython notebook.

Does anyone know how I can do this efficiently? Thank you.

import matplotlib.pyplot as plt

#data
df = pd.DataFrame(np.random.randint(0,100,size=(1000, 4)), columns=list('ABCD'))
#rolling mean
df_mu = df.rolling(window=100).mean()
#rolling standard deviation
df_sd = df.rolling(window=100).std()

for col in df.columns:
    plt.plot(df.index.values, df[col], 'r--', df_mu[col], 'b--', df_sd[col], 'g--')
    plt.title(col)
    plt.savefig('C:/Users/test/Google Drive/Working Folder')
    plt.close()    

I have tried but it doesn't plot in my ipython notebook nor does it save anything. What am I doing wrong?

Upvotes: 0

Views: 105

Answers (1)

Ali
Ali

Reputation: 1635

A line chart could be produced by the following few lines:

import matplotlib.pyplot as plt

for col in df.columns:
    plt.plot(df.index.values, df[col], 'r--', df_mu[col], 'b--', df_sd[col], 'g--')
    plt.title(col)
    plt.savefig('/path/to/store/plots')
    plt.close()     

Upvotes: 1

Related Questions