import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(x)
def interp():
f_linear = interpolate.interp1d(x, y)
f_cubic = interpolate.interp1d(x, y, kind='cubic')
f_lagrange=interpolate.lagrange(x,y)
xnew = np.linspace(0, 10, num=40, endpoint=True)
ynew_linear=f_linear(xnew)
ynew_cubic=f_cubic(xnew)
ynew_lagrange=f_lagrange(xnew)
sp1,sp2,sp3=plt.subplot(3,1,1),plt.subplot(3,1,2),plt.subplot(3,1,3)
sp1.plot(x,y,'o',xnew,ynew_linear,'r--')
sp1.set_title('linear')
sp1.legend(['data','linear'],loc='best')
sp2.plot(x, y, 'o', xnew, ynew_cubic, 'g--')
sp2.set_title('cubic')
sp2.legend(['data', 'cubic'], loc='best')
sp3.plot(x, y, 'o', xnew, ynew_lagrange, 'b--')
sp3.set_title('lagrange')
sp3.legend(['data', 'lagrange'], loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
if __name__ == '__main__':
interp()
