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

0 投票
1 回答
3380 浏览
提问于 2025-04-16 23:34

我正在按照这个问题中的建议来处理以下数据:

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 个回答

2

你在用哪个版本的numpy和scipy呢?我的代码在numpy 1.6.0和scipy 0.9.0下可以正常运行(我把linspace的导入从scipy.interpolate移到了numpy里),然后我还加了一个散点图:

enter image description here

撰写回答