Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo of using matplotlib to calculate and display the autocorrelation of a time series stored in a list.
import matplotlib.pyplot as plt
import numpy as np
d=[1.04,1.04,1.16,1.22,1.46,2.34,1.16,1.12,1.24,1.30,1.44,1.22,1.26,1.34,1.26,1.40,1.52,2.56,1.36,1.30,1.20,1.12,1.12,1.12,1.06,1.06,1.00,1.02,1.04,1.02,1.06,1.02,1.04,0.98,0.98,0.98,1.00,1.02,1.02,1.00,1.02,0.96,0.94,0.94,0.94,0.96,0.86,0.92,0.98,1.08,1.04,0.74,0.98,1.02,1.02,1.12,1.34,2.02,1.68,1.12,1.38,1.14,1.16,1.22,1.10,1.14,1.16,1.28,1.44,2.58,1.30,1.20,1.16,1.06,1.06,1.08,1.00,1.00,0.92,1.00,1.02,1.00,1.06,1.10,1.14,1.08,1.00,1.04,1.10,1.06,1.06,1.06,1.02,1.04,0.96,0.96,0.96,0.92,0.84,0.88,0.90,1.00,1.08,0.80,0.90,0.98,1.00,1.10,1.24,1.66,1.94,1.02,1.06,1.08,1.10,1.30,1.10,1.12,1.20,1.16,1.26,1.42,2.18,1.26,1.06,1.00,1.04,1.00,0.98,0.94,0.88,0.98,0.96,0.92,0.94,0.96,0.96,0.94,0.90,0.92,0.96,0.96,0.96,0.98,0.90,0.90,0.88,0.88,0.88,0.90,0.78,0.84,0.86,0.92,1.00,0.68,0.82,0.90,0.88,0.98,1.08,1.36,2.04,0.98,0.96,1.02,1.20,0.98,1.00,1.08,0.98,1.02,1.14,1.28,2.04,1.16,1.04,0.96,0.98,0.92,0.86,0.88,0.82,0.92,0.90,0.86,0.84,0.86,0.90,0.84,0.82,0.82,0.86,0.86,0.84,0.84,0.82,0.80,0.78,0.78,0.76,0.74,0.68,0.74,0.80,0.80,0.90,0.60,0.72,0.80,0.82,0.86,0.94,1.24,1.92,0.92,1.12,0.90,0.90,0.94,0.90,0.90,0.94,0.98,1.08,1.24,2.04,1.04,0.94,0.86,0.86,0.86,0.82,0.84,0.76,0.80,0.80,0.80,0.78,0.80,0.82,0.76,0.76,0.76,0.76,0.78,0.78,0.76,0.76,0.72,0.74,0.70,0.68,0.72,0.70,0.64,0.70,0.72,0.74,0.64,0.62,0.74,0.80,0.82,0.88,1.02,1.66,0.94,0.94,0.96,1.00,1.16,1.02,1.04,1.06,1.02,1.10,1.22,1.94,1.18,1.12,1.06,1.06,1.04,1.02,0.94,0.94,0.98,0.96,0.96,0.98,1.00,0.96,0.92,0.90,0.86,0.82,0.90,0.84,0.84,0.82,0.80,0.80,0.76,0.80,0.82,0.80,0.72,0.72,0.76,0.80,0.76,0.70,0.74,0.82,0.84,0.88,0.98,1.44,0.96,0.88,0.92,1.08,0.90,0.92,0.96,0.94,1.04,1.08,1.14,1.66,1.08,0.96,0.90,0.86,0.84,0.86,0.82,0.84,0.82,0.84,0.84,0.84,0.84,0.82,0.86,0.82,0.82,0.86,0.90,0.84,0.82,0.78,0.80,0.78,0.74,0.78,0.76,0.76,0.70,0.72,0.76,0.72,0.70,0.64]
y=[]
tot=0
min=999
for i in d:
y.append(float(i))
tot=tot+float(i)
if (min>float(i)):
min=float(i)
av=tot/len(y)
z=[]
m=[]
for i in y:
z.append(i-av)
m.append(i-min)
fig = plt.figure()
#plt.title('Google Search Trend: "flowers"')
ax1 = fig.add_subplot(211)
ax1.plot(y)
#ax1.set_title('test')
ax1.set_ylabel('Search trend volume')
ax2 = fig.add_subplot(212)
ax2.set_title('Google Trends data: "flowers"')
ax2.acorr( z,usevlines=True,maxlags=None,normed=True,lw=2)
ax2.grid(True)
ax2.axhline(0,color='black',lw=2)
ax2.set_ylabel('Autocorrelation')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.