Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib draw Spline from multiple points

I have array points

nodes = [(1, 2), (6, 15), (10, 6), (10, 3), (3, 7)]

And now, I need draw Spline passing through the points. You can see image result enter image description here

But I don't know how to draw with matplotlib.pyplot. Help me

like image 573
Duc Tran Avatar asked Feb 10 '23 08:02

Duc Tran


1 Answers

So , the right piece of code is:

from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate


nodes = np.array( [ [1, 2], [6, 15], [10, 6], [10, 3], [3, 7] ] )

x = nodes[:,0]
y = nodes[:,1]

tck,u     = interpolate.splprep( [x,y] ,s = 0 )
xnew,ynew = interpolate.splev( np.linspace( 0, 1, 100 ), tck,der = 0)

plt.plot( x,y,'o' , xnew ,ynew )
plt.legend( [ 'data' , 'spline'] )
plt.axis( [ x.min() - 1 , x.max() + 1 , y.min() - 1 , y.max() + 2 ] )
plt.show()

image

like image 167
George Avatar answered Feb 13 '23 03:02

George