2011-08-14 40 views
1

我按照this question以下数据给出的建议是:情节光滑曲线在PyPlot与大Y值

import matplotlib.pyplot as plt 
from numpy import array, linspace 
from scipy.interpolate import spline 

xdata = array([25, 36, 49]) 
ydata = array([145247, 363726, 789055]) 

xnew = linspace(xdata.min(),xdata.max(),300) 

ysmooth = spline(xdata,ydata,xnew) 

plt.plot(xnew,ysmooth) 
plt.show() 

虽然它工作正常,在这个问题的数据,由于某种原因,这个数据,它打破:

Traceback (most recent call last): 
    File "test.py", line 526, in <module> 
    ysmooth = spline(xdata,ydata,xnew) 
    File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 809, in spline 
    return spleval(splmake(xk,yk,order=order,kind=kind,conds=conds),xnew) 
    File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 771, in splmake 
    coefs = func(xk, yk, order, conds, B) 
    File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 500, in _find_smoothest 
    p = np.dual.solve(Q,tmp) 
    File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/linalg/basic.py", line 70, in solve 
    raise LinAlgError("singular matrix") 
numpy.linalg.linalg.LinAlgError: singular matrix 

我该如何解决这个问题?这似乎是非常简单的算法适合的数据。

回答

2

你使用的是哪些版本的numpy和scipy?您的代码对我的作品与numpy的1.6.0和0.9.0 SciPy的(移动linspace从进口到scipy.interpolate后NumPy的),加入散点图后:

enter image description here

+0

有趣。我在Mac上使用最新的Enthought安装。 –

+0

我想我不得不在这种情况下去。我花了太多时间让这张图看起来已经正确。 –

+0

其实'2p度'np.polyfit'结束了工作和拟合甚至比这更好。所以我想这一切都很好。 –