【问题标题】:matplotlib 1.4.0 in PyQt5 application results in Segmentation FaultPyQt5 应用程序中的 matplotlib 1.4.0 导致分段错误
【发布时间】:2014-08-27 22:16:51
【问题描述】:

在 Plot 窗口正确显示后,在 PyQt5 RAD 应用程序中使用 matplotlib 会导致分段错误。系统:Fedora 19 3.14.17,matplotlib-1.4.0.tar.gz 是从matplotlib.org 下载的,构建在这个系统上。

应用程序有一个连接到“doactionpushbutton”功能的按钮,当点击该功能时,带有“图 1”标签的绘图被打开并且是正确的。窗口保持打开状态,直到光标移到窗口上,此时绘图窗口和主应用程序窗口都关闭并返回分段错误消息。

由于我是 mathplotlib 的新手,所以问题是“我做错了什么?”

提前感谢您的帮助。

注意:我提供了一个缩小尺寸的完整程序来演示该问题。

适用的代码如下:

主窗口

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Thu Aug 28 18:24:05 2014
#      by: PyQt5 UI code generator 5.3.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 20, 97, 31))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 27))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

计划

import sys
import numpy as np
from PyQt5 import QtWidgets, QtGui, QtWidgets
import matplotlib
#matplotlib.use('')
import matplotlib.pyplot as plt


from mainwindow import Ui_MainWindow

class Editor(QtWidgets.QMainWindow):

    def __init__(self):
        super(Editor, self).__init__()
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.show()
        self.ui.pushButton.clicked.connect(self.doactionpushButton)

    def doactionpushButton(self):
        self.plot_it()

    def plot_it(self):
        # red dashes, blue squares and green triangles
        t = np.arange(0., 5., 0.2)
        plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
        plt.show(block=True)


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Editor()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

【问题讨论】:

  • 您能否将其简化为不使用Ui_MainWindow 的示例。就目前而言,我无法运行此示例,因此不知道问题是在 mpl 方面还是在我们看不到的代码中。我怀疑问题是你也设置了 pyqt4 并且 pyplot 正在为 pyqt4 启动,因此事情会爆炸。在导入 pyplot 之前尝试添加 import matplotlibmatplotlib.use('qt5agg')
  • 谢谢-尝试了您建议的“导入”和“使用”。尽管 PyQt 网站说 PyQt4 和 PyQt5 可以一起生活,但系统响应如下。将尝试组合一个更紧凑的失败示例。 ValueError: Unrecognized backend string "qt5agg": 有效字符串是 ['pgf', 'cairo', 'MacOSX', 'CocoaAgg', 'gdk', 'ps', 'GTKAgg', 'GTK', 'QtAgg', '模板','FltkAgg','emf','GTK3Cairo','GTK3Agg','WX','Qt4Agg','TkAgg','agg','svg','GTKCairo','WXAgg','pdf' ]
  • 我怀疑它'QtAgg'然后。
  • @tcaswell QtAgg 要我安装 pyqt3。试过了,反正我可以指定pyqt5吗?我尝试了 import PyQt5,但是当我安装它时,python 似乎拿起了 pyqt3。
  • @tcaswell 演示问题的完整代码在原始帖子中。感谢您的帮助。

标签: matplotlib pyqt5


【解决方案1】:

下载 matplotlib-master.zip 并安装它可以解决问题。由于某种原因,matplotlib (1.4.0) 的版本没有 tcaswell 进行的更正。 感谢 tcaswell 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 2014-06-22
    • 2013-04-11
    • 1970-01-01
    • 2015-06-09
    • 2016-02-11
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多