import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(10, dtype=float)
y = np.zeros_like(x)
y[5:] = 1.
plt.plot(x,y,'ro')
plt.axis('equal');
# Simple (default) spline
cs = make_interp_spline(x,y)
# It returns a function
cs
cs(3.5)
cs([5.3, 2.11])
xx = np.linspace(x[0],x[-1],100)
yy = [cs(x) for x in xx]
plt.plot(xx,yy)
plt.plot(x,y, 'ro')
plt.axis('equal');
# Spline with specified boundary conditions
left = [(1,1)] # First deriv = 1
right = [(1,-10.2)] # First deriv = -10.2
cs = make_interp_spline(x,y, bc_type=(left, right))
xx = np.linspace(x[0],x[-1],100)
yy = [cs(x) for x in xx]
plt.plot(xx,yy)
plt.plot(x,y, 'ro')
plt.axis('equal');
y = [4, 8, 7, 5, 1, 2, 9, 7, 8, 9, 0]
N = len(y)
x = np.arange(300, N+300)
plt.plot(x, y, 'ro');
cs = make_interp_spline(x, y)
xx = np.linspace(x[0], x[-1], 100)
plt.plot(x, y, 'ro')
plt.plot(xx, cs(xx));
#plt.savefig('spline_example.png')