2011-08-14 49 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'結束了工作和擬合甚至比這更好。所以我想這一切都很好。 –