在具有大Y值的PyPlot中绘制平滑曲线

时间:2011-08-14 02:06:35

标签: python scipy matplotlib curvesmoothing

我正在遵循this question中给出的以下数据的建议:

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

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

1 个答案:

答案 0 :(得分:2)

您使用的是什么版本的numpy和scipy?在添加散点图之后,你的代码适用于numpy 1.6.0和scipy 0.9.0(在将linspace导入从scipy.interpolate移动到numpy之后):

enter image description here