Matplotlib 从多个点绘制样条曲线 [英] Matplotlib draw Spline from multiple points

查看:42
本文介绍了Matplotlib 从多个点绘制样条曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组点

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

现在,我需要绘制通过这些点的 Spline.你可以看到图像结果

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

但我不知道如何用 matplotlib.pyplot 绘图.帮帮我

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

推荐答案

因此,正确的代码段是:

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()

这篇关于Matplotlib 从多个点绘制样条曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆