Matplotlib从多个点绘制样条曲线

时间:2015-04-24 02:45:13

标签: python matplotlib

我有阵列点

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

现在,我需要通过积分来绘制Spline。你可以看到图像结果 enter image description here

但我不知道如何用matplotlib.pyplot画画。帮帮我

1 个答案:

答案 0 :(得分:5)

所以,正确的代码是:

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