如何在Matplotlib中绘制三次样条 [英] How to draw cubic spline in matplotlib

查看:133
本文介绍了如何在Matplotlib中绘制三次样条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用平滑线(例如三次样条线)连接以下points

I want to connect the following points using smooth line, say cubic spline

points = [(3.28,0.00),(4.00,0.50),(4.40,1.0),(4.60,1.52),(5.00,2.5),(5.00,3.34),(4.70,3.8)]
points = points + [(4.50,3.96),(4.20,4.0),(3.70,3.90),(3.00,3.5),(2.00,2.9)]

最后得到像这样的橙色线(这是使用矢量绘图语言渐近线创建的)

and finally get orange line like this (this one is created using a vector plotting language Asymptote)

我想知道如何在matplotlib中以一种简单的方式进行操作.我已经看过类似的问题,例如使用matplotlib生成平滑线形图,但是直接使用该方法会产生如下图这

I'm wondering how to do it in matplotlib in a simple way. I already had a look at similar question, e.g. Generating smooth line graph using matplotlib, but direct use of that method produces figure like this

那当然不是我想要的.

推荐答案

您需要采用如下所示的参数化方法:

You need to take a parametric approach, like this:

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

points = [(3.28,0.00),(4.00,0.50),(4.40,1.0),(4.60,1.52),(5.00,2.5),(5.00,3.34),(4.70,3.8)]
points = points + [(4.50,3.96),(4.20,4.0),(3.70,3.90),(3.00,3.5),(2.00,2.9)]
data = np.array(points)

tck,u = interpolate.splprep(data.transpose(), s=0)
unew = np.arange(0, 1.01, 0.01)
out = interpolate.splev(unew, tck)

plt.figure()
plt.plot(out[0], out[1], color='orange')
plt.plot(data[:,0], data[:,1], 'ob')
plt.show()

这基本上是从 查看全文

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