I have time-series data that collected weekly basis, where I want to see the correlation of its two columns. to do so, I could able to find a correlation between two columns and want to see how rolling correlation moves each year. my current approach works fine but I need to normalize the two columns before doing rolling correlation and making a line plot. In my current attempt, I don't know how to show 3-year, 5 year rolling correlation. Can anyone suggest a possible idea of doing this in matplotlib?
current attempt:
Here is my current attempt:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
dataPath="https://gist.github.com/jerry-shad/503a7f6915b8e66fe4a0afbc52be7bfa#file-sample_data-csv"
def ts_corr_plot(dataPath, roll_window=4):
df = pd.read_csv(dataPath)
df['Date'] = pd.to_datetime(df['Date'])
df['week'] = pd.DatetimeIndex(df['date']).week
df['year'] = pd.DatetimeIndex(df['date']).year
df['week'] = df['date'].dt.strftime('%W').astype('uint8')
def find_corr(x):
df = df.loc[x.index]
return df[:, 1].corr(df[:, 2])
df['corr'] = df['week'].rolling(roll_window).apply(find_corr)
fig, ax = plt.subplots(figsize=(7, 4), dpi=144)
sns.lineplot(x='week', y='corr', hue='year', data=df,alpha=.8)
plt.show()
plt.close
update:
I want to see rolling correlation in different time window such as:
plt_1 = ts_corr_plot(dataPath, roll_window=4)
plt_2 = ts_corr_plot(dataPath, roll_window=12)
plt_3 = ts_corr_plot(dataPath, roll_window=24)
I need to add 3-years, 5-years rolling correlation to the plots but I couldn't find a better way of doing this. Can anyone point me out how to make a rolling correlation line plot for time series data? How can I improve the current attempt? any idea?
desired plot
this is my expected plot that I want to obtain:

