Skip to content

Instantly share code, notes, and snippets.

@koljamaier
Created July 25, 2017 10:06
Show Gist options
  • Save koljamaier/4bb6ef41fe06547a025431a861d42832 to your computer and use it in GitHub Desktop.
Save koljamaier/4bb6ef41fe06547a025431a861d42832 to your computer and use it in GitHub Desktop.
Visualization of a moving average
from __future__ import division
import pylab
import numpy as np
"""
This script visualizes a trend in linear-like-shaped data
generated with random noise.
"""
# generate linear like function (with noise)
x = np.arange(100)
delta = np.random.uniform(-10,10, size=(100,))
y = .4 * x +3 + delta
def movingaverage(interval, window_size):
window= np.ones(int(window_size))/float(window_size)
return np.convolve(interval, window, 'same')
pylab.plot(x,y,"k.")
y_av_5 = movingaverage(y, 2)
y_av_20 = movingaverage(y, 10)
pylab.plot(x, y_av_5,"--", label=r"$n=20$")
pylab.plot(x, y_av_20, label=r"$n=20$")
pylab.legend(loc='upper left')
pylab.xlim(0,100)
pylab.xlabel("time")
pylab.ylabel("data")
#pylab.grid(True)
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment