Plot Smooth Curve in PyPlot with Large Y Values
I'm following the advice given in this question for the following data:
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()
While it works fine for the data in that question, for some reason, with this data, it breaks:
Traceback (most recent call last):
File "test.py", line 526, in <module>
ysmooth = spline(xdata,ydata,xnew)
File "/Library/Frameworks/Python.framework/Versio开发者_高级运维ns/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
How do I fix this? This seems like really easy data for the algorithm to fit.
What versions of numpy and scipy are you using? Your code works for me with numpy 1.6.0 and scipy 0.9.0 (after moving the linspace import to numpy from scipy.interpolate), after adding a scatterplot:
精彩评论