使用scipy内插一条闭合曲线 [英] Interpolating a closed curve using scipy

查看:148
本文介绍了使用scipy内插一条闭合曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python脚本,以用样条曲线插值给定的一组点.这些点由它们的[x, y]坐标定义.

I'm writing a python script to interpolate a given set of points with splines. The points are defined by their [x, y] coordinates.

我尝试使用此代码:

x = np.array([23, 24, 24, 25, 25])
y = np.array([13, 12, 13, 12, 13])
tck, u = scipy.interpolate.splprep([x,y], s=0)
unew = np.arange(0, 1.00, 0.005)
out = scipy.interpolate.splev(unew, tck) 

这给了我这样的曲线:

但是,我需要有一条平滑的闭合曲线-在上方的图片中,明显的一点上的导数并不相同. 我该如何实现?

However, I need to have a smooth closed curve - on the picture above the derivatives at one of the points are obviously not the same. How can I achieve this?

推荐答案

您的闭合路径可以视为参数曲线, x = f(u) y = g(u ),其中 u 是沿曲线的距离,以区间 [0,1)为界.您可以使用 scipy.interpolate.splprep per=True将您的xy点视为周期性,然后使用

Your closed path can be considered as a parametric curve, x=f(u), y=g(u) where u is distance along the curve, bounded on the interval [0, 1). You can use scipy.interpolate.splprep with per=True to treat your x and y points as periodic, then evaluate the fitted splines using scipy.interpolate.splev:

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

x = np.array([23, 24, 24, 25, 25])
y = np.array([13, 12, 13, 12, 13])

# append the starting x,y coordinates
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]

# fit splines to x=f(u) and y=g(u), treating both as periodic. also note that s=0
# is needed in order to force the spline fit to pass through all the input points.
tck, u = interpolate.splprep([x, y], s=0, per=True)

# evaluate the spline fits for 1000 evenly spaced distance values
xi, yi = interpolate.splev(np.linspace(0, 1, 1000), tck)

# plot the result
fig, ax = plt.subplots(1, 1)
ax.plot(x, y, 'or')
ax.plot(xi, yi, '-b')

这篇关于使用scipy内插一条闭合曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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