问题描述
我一直在尝试绘制一条线以及样条曲线拟合.以下是我的代码的通用版本."x_coord"和"y_coord"是包含浮点值列表的列表.
I have been attempting to plot a line, along with a spline fitting. The following is a generalised version of my code. 'x_coord' and 'y_coord' are lists containing lists of float values.
import matplotlib.pyplot as plt
from scipy import interpolate as ipl
for a in range(len(x_coord)):
plt.plot(x_coord[a],y_coord[a],label='Label')
yinterp = ipl.UnivariateSpline(x_coord[a],y_coord[a],s=1e4)(x_coord[a])
plt.plot(x_coord[a],yinterp,label='Spline Fit')
虽然我相信这过去对我有用,但现在我收到一条错误消息:
While I believe this has worked for me in the past, I now obtain an error message:
/.../Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/interpolate/fitpack2.pyc in __init__(self, x, y, w, bbox, k, s, ext)
165
166 data = dfitpack.fpcurf0(x,y,k,w=w,
--> 167 xb=bbox[0],xe=bbox[1],s=s)
168 if data[-1] == 1:
169 # nest too small, setting to maximum bound
error: (m>k) failed for hidden m: fpcurf0:m=0
我看到了类似错误消息的情况(例如 dfitpack.error:(m> k)对于隐藏的m:fpcurf0:m = 1 )失败,只有在特定情况下,似乎存在涉及字典的问题,而我的代码中没有使用字典.
I have seen cases of similar error messages (e.g. dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=1), only in that particular case there seemed to be problems involving dictionaries, of which none are used in my code.
任何对此事的建议将不胜感激.
Any advice on this matter would be greatly appreciated.
推荐答案
您正在尝试实例化具有零长度数组的 UnivariateSpline
对象
You are trying to instantiate a UnivariateSpline
object with a zero-length array
>>> from scipy.interpolate import UnivariateSpline
>>> UnivariateSpline([], [])
<snip>
dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=0
>>>
>>> UnivariateSpline([1], [2])
Traceback (most recent call last):
<snip>
dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=1
该错误很可能是由此行触发的,它将隐藏变量 m
设置为 x
的长度,并检查您在 k 处至少有
k + 1
个点code>是样条曲线度(默认为三次,k = 3).
The error is likely triggered by this line, which sets a hidden variable
m
to the length of x
and checks that you have at least k+1
points where k
is the spline degree (default is cubic, k=3).
>>> spl = UnivariateSpline(range(4), range(4))
>>> spl(2)
array(2.0)
这篇关于Python:interpolate.UnivariateSpline软件包“错误:(m> k)对于隐藏的m:fpcurf0:m = 0"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!