Skip to content

Instantly share code, notes, and snippets.

@habi
Created September 11, 2014 08:27
Show Gist options
  • Save habi/c0d70ff71c21720ad945 to your computer and use it in GitHub Desktop.
Save habi/c0d70ff71c21720ad945 to your computer and use it in GitHub Desktop.
Interpolating data
'''
Smoothing/Interpolation-example, based on
https://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/interpolate.html
'''
from __future__ import division
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
import time
plt.ion()
for i in range(4, 15):
plt.cla()
x = np.linspace(0, 10, i)
y = np.cos(-x ** 2 / 8)
f = interp1d(x, y + 0.25 )
f2 = interp1d(x, y + 0.5, kind='cubic')
xnew = np.linspace(0, 10, 400)
xx = np.linspace(0, 10, 100)
yy = np.cos(-xx ** 2 / 8)
plt.plot(x,y,'bo')
plt.plot(xx,yy,'b')
plt.plot(xnew,f(xnew),'r-')
plt.plot(xnew, f2(xnew),'g--')
plt.legend(['data points', 'data', 'shifted linear int',
'shifted cubic int'], loc=3)
plt.title(' '.join(['interpolating', str(i), 'points']))
plt.ylim([-2,2])
plt.draw()
time.sleep(1)
plt.ioff()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment