import numpy as np
from scipy import interpolate

import matplotlib.pyplot as plt

#x = np.arange(0, 2*np.pi+np.pi/4, 2*np.pi/8)
#y = np.sin(x)

ctr =np.array( [(3 , 1, 1), (2.5, 4, 2), (0, 1, 3), (-2.5, 4, 2),
                (-3, 0, 1.5), (-2.5, -4, 2), (0, -1, 4), (2.5, -4, 5), (3, -1, 4)])

x=ctr[:,0]
y=ctr[:,1]
z = ctr[:,2]

#x=np.append(x,x[0])
#y=np.append(y,y[0])

tck,u = interpolate.splprep([x,y],k=3,s=0)
u=np.linspace(0,1,num=50,endpoint=True)
out = interpolate.splev(u,tck)

plt.figure()
plt.plot(x, y, 'ro', out[0], out[1], 'b')
plt.legend(['Points', 'Interpolated B-spline', 'True'],loc='best')
plt.axis([min(x)-1, max(x)+1, min(y)-1, max(y)+1])
plt.title('B-Spline interpolation')
plt.show()

from mpl_toolkits import mplot3d

tck,u = interpolate.splprep([x,y,z],k=3,s=0)
u=np.linspace(0,1,num=50,endpoint=True)
out = interpolate.splev(u,tck)

fig = plt.figure()
ax = plt.axes(projection='3d')
# plt.figure()
plt.plot(x, y, z, 'ro')
plt.plot( out[0], out[1], out[2], 'b')
plt.legend(['Points', 'Interpolated B-spline', 'True'],loc='best')
# plt.axis([min(x)-1, max(x)+1, min(y)-1, max(y)+1])
plt.title('B-Spline interpolation')
plt.show()