I'm designing a GUI with PyQt where I need to display a matplotlib/pylab window when I click on a button that makes the plot of the data from a function I've created. It's like a runtime used in Matlab. I want to keep the matplotlib/pylab window as my window everytime I press that button.

Here is a basic example that will plot three different samples using a QThread:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import random

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

from PyQt4 import QtGui, QtCore

class MatplotlibWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MatplotlibWidget, self).__init__(parent)

        self.figure = Figure()
        self.canvas = FigureCanvasQTAgg(self.figure)

        self.axis = self.figure.add_subplot(111)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.canvas)

class ThreadSample(QtCore.QThread):
    newSample = QtCore.pyqtSignal(list)

    def __init__(self, parent=None):
        super(ThreadSample, self).__init__(parent)

    def run(self):
        randomSample = random.sample(range(0, 10), 10)

        self.newSample.emit(randomSample)

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonPlot = QtGui.QPushButton(self)
        self.pushButtonPlot.setText("Plot")
        self.pushButtonPlot.clicked.connect(self.on_pushButtonPlot_clicked)

        self.matplotlibWidget = MatplotlibWidget(self)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonPlot)
        self.layoutVertical.addWidget(self.matplotlibWidget)

        self.threadSample = ThreadSample(self)
        self.threadSample.newSample.connect(self.on_threadSample_newSample)
        self.threadSample.finished.connect(self.on_threadSample_finished)

    @QtCore.pyqtSlot()
    def on_pushButtonPlot_clicked(self):
        self.samples = 0
        self.matplotlibWidget.axis.clear()
        self.threadSample.start()

    @QtCore.pyqtSlot(list)
    def on_threadSample_newSample(self, sample):
        self.matplotlibWidget.axis.plot(sample)
        self.matplotlibWidget.canvas.draw()

    @QtCore.pyqtSlot()
    def on_threadSample_finished(self):
        self.samples += 1
        if self.samples <= 2:
            self.threadSample.start()

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(666, 333)
    main.show()

    sys.exit(app.exec_())

image

  • You are generating the data for plotting inside your class (with the random generator). Is it possible to pass the data from outside. Something like that: main = MyWindow(data) – jonie83 Sep 3 '14 at 15:42 ::...
    免责声明:
    当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
    内容版权归原作者所有;
    本人对内容的有效性/合法性不承担任何强制性责任.
    若有不妥, 欢迎评注提醒:

    或是邮件反馈可也:
    askdama[AT]googlegroups.com


    订阅 substack 体验古早写作:


    点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

    关注公众号, 持续获得相关各种嗯哼:
    zoomquiet


    自怼圈/年度番新

    DU22.4
    关于 ~ DebugUself with DAMA ;-)
    粤ICP备18025058号-1
    公安备案号: 44049002000656 ...::