Matplotlib draw()在MacOS上的pyqt环境中不起作用

时间:2019-03-15 10:40:03

标签: python matplotlib draw

我正在尝试更新嵌入在pyqt5窗口中的matplotlib图。 我使用了self.canvas.draw()(在update函数中)来更新函数中的绘图以重绘图形。这在Window上工作正常,但在MacOS上不工作。 我不明白为什么以及是否存在解决方案?

事实上,我需要单击轴才能更新该图……很奇怪。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import numpy as np
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

class StimEdit(QMainWindow):
    def __init__(self, parent=None):
        super(StimEdit, self).__init__()
        self.parent = parent

        self.centralWidget = QWidget()
        self.color = self.centralWidget.palette().color(QPalette.Background)
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QHBoxLayout()

        self.set_Stims()
        self.mainHBOX_param_scene.addWidget(self.Stimulation)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

    def set_Stims(self):
        self.Stimulation = QGroupBox('Stim N°1')

        self.layout_Stimulation_courbe = QVBoxLayout()
        self.layout_Stimulation = QHBoxLayout()

        self.button = QPushButton('push')
        self.masceneparam = paramViewer(self)
        self.button.clicked.connect(self.button_fun)

        self.layout_Stimulation_courbe.addWidget(self.masceneparam)
        self.layout_Stimulation_courbe.addWidget(self.button)
        self.Stimulation.setLayout(self.layout_Stimulation_courbe)

    def button_fun(self):
        self.masceneparam.update()
        self.parent.processEvents()




class paramViewer(QGraphicsView):
    def __init__(self, parent=None):
        super(paramViewer, self).__init__(parent)
        self.parent=parent
        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.axes_stim=self.figure.add_subplot(111)
        self.canvas.setGeometry(0, 0, 1600, 500 )
        layout = QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.canvas.show()


    def update(self):
        self.axes_stim.clear()
        t = np.arange(1000)/100
        self.axes_stim.plot(t,np.sin(2*np.pi*np.random.uniform()*100*t))
        self.canvas.draw()


def main():
    app = QApplication(sys.argv)
    ex = StimEdit(app)
    ex.setWindowTitle('St ')
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

编辑

我尝试使用matplotlib的示例从@ImportanceOfBeingErnest在Qt(https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html)中嵌入图形

# from PyQt5.QtGui import *
# from PyQt5.QtWidgets import *
import numpy as np
import sys
import matplotlib.pyplot as plt
from matplotlib.figure import Figure

from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
if is_pyqt5():
    from matplotlib.backends.backend_qt5agg import (
        FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
    from matplotlib.backends.backend_qt4agg import (
        FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure

class StimEdit(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(StimEdit, self).__init__()
        self.parent = parent

        self.centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtWidgets.QHBoxLayout()

        self.set_Stims()
        self.mainHBOX_param_scene.addWidget(self.Stimulation)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

    def set_Stims(self):
        self.Stimulation = QtWidgets.QGroupBox('Stim N°1')

        self.layout_Stimulation_courbe = QtWidgets.QVBoxLayout()
        self.layout_Stimulation = QtWidgets.QHBoxLayout()

        self.button = QtWidgets.QPushButton('push')
        self.masceneparam = paramViewer(self)
        self.button.clicked.connect(self.button_fun)

        self.layout_Stimulation_courbe.addWidget(self.masceneparam)
        self.layout_Stimulation_courbe.addWidget(self.button)
        self.Stimulation.setLayout(self.layout_Stimulation_courbe)

    def button_fun(self):
        self.masceneparam.update()
        self.parent.processEvents()




class paramViewer(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(paramViewer, self).__init__(parent)
        self.parent=parent
        self.scene = QtWidgets.QGraphicsScene(self)
        self.setScene(self.scene)
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure )
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.axes_stim=self.figure.add_subplot(111)
        self.canvas.setGeometry(0, 0, 1600, 500 )
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.canvas.show()


    def update(self):
        self.axes_stim.clear()
        t = np.arange(1000)/100
        self.axes_stim.plot(t,np.sin(2*np.pi*np.random.uniform()*100*t))
        self.canvas.figure.canvas.draw()
        # self.canvas.draw()


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = StimEdit(app)
    ex.setWindowTitle('St ')
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

但是问题仍然存在。

1 个答案:

答案 0 :(得分:0)

@ImportanceOfBeingErnest的答案

由于我没有使用Mac的经验,因此在这里可能无法进一步提供帮助,但是最后的建议可能是将.draw()替换为.draw_idle()。 (在这种情况下,两者都应该起作用,但您永远不知道...)