为什么向matplotlib图形添加子图形会导致numpy崩溃



我在QtDesigner中创建了一个对话框窗口,其中包含一个名为plot_layout的布局的小部件。

当运行此代码时,Numpy执行时不会出现任何问题:请注意,self.ax1 = fig1.add_subplot(111)已被注释掉。

from PyQt5 import QtWidgets, uic
import os
import sys
import numpy as np
from pathlib import Path
from os.path import join
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

dir = Path(os.getcwd())
parent_dir = os.path.normpath(dir.parent)
sys.path.append(parent_dir)
class Test(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
uic.loadUi(join(parent_dir, r'sub_windowstest.ui'), self)
fig1 = Figure()
# self.ax1 = fig1.add_subplot(111)
canvas1 = FigureCanvas(fig1)
self.plot_layout.addWidget(canvas1)
toolbar = NavigationToolbar(canvas1, self.widget, coordinates=True)
self.plot_layout.addWidget(toolbar)
self.pushButton.clicked.connect(self.action)
def action(self):
c = np.polyfit(np.arange(1600), np.arange(1600) + 1, deg=1)
print(c)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())

现在,在取消对self.ax1 = fig1.add_subplot(111)的注释后,出现以下错误:

** On entry to DLASCLS parameter number  4 had an illegal value
** On entry to DLASCLS parameter number  4 had an illegal value
** On entry to DLASCLS parameter number  4 had an illegal value
** On entry to DLASCLS parameter number  4 had an illegal value
** On entry to DLASCLS parameter number  5 had an illegal value
** On entry to DLASCLS parameter number  4 had an illegal value
Traceback (most recent call last):
File "test.py", line 31, in action
c = np.polyfit(np.arange(1600), np.arange(1600) + 1, deg=1)
File "<__array_function__ internals>", line 5, in polyfit
File "C:Python38libsite-packagesnumpylibpolynomial.py", line 629, in polyfit
c, resids, rank, s = lstsq(lhs, rhs, rcond)
File "<__array_function__ internals>", line 5, in lstsq
File "C:Python38libsite-packagesnumpylinalglinalg.py", line 2306, in lstsq
x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
File "C:Python38libsite-packagesnumpylinalglinalg.py", line 100, in _raise_linalgerror_lstsq
raise LinAlgError("SVD did not converge in Linear Least Squares")
numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares

我在另外两台运行相同版本python的计算机上尝试过同样的代码,它运行起来没有问题。关于如何在我当前的系统上诊断这个问题,有什么建议吗?我运行的是Python 3.7.8 64位,Windows 10 Pro 64位,所有软件包都是最新版本,英特尔酷睿i5-8400。注意:我也试过重新安装python,甚至安装python 3.8,但结果都是一样的。

我认为这是numpy中的一个错误。您的问题可能是np.polyfit与某些matplotlib图表不兼容。即使用np.polynomial.polynomial.polyfit替换它也无济于事。

在我的例子中,我通过用我自己的最小二乘线性回归拟合代替这个函数来解决这个问题:

def linreg(x, y):
meanx = np.mean(x)
meany = np.mean(y)
sumxx2 = np.sum((x - meanx) ** 2)
sumxy = np.sum(((x - meanx) * (y - meany)))
m = sumxy / sumxx2
c = meany - m * meanx
return np.array([c, m])  # though, you might want to return as np.array([m, c]) as np.polyfit would

另请参阅:Python:创建绘图导致未来numpy.linalg.ein偏离

最新更新