Skip to content Skip to sidebar Skip to footer

How To Plot On My Gui

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 r

Solution 1:

This is code from user1006989 (best answer) adapted to PyQt5, hopefully it will be useful to someone:

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_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

from PyQt5 import QtCore #conda install pyqtfrom PyQt5 import QtWidgets

classMatplotlibWidget(QtWidgets.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 = QtWidgets.QVBoxLayout(self)#QVBoxLayout
        self.layoutVertical.addWidget(self.canvas)

classThreadSample(QtCore.QThread):
    newSample = QtCore.pyqtSignal(list)

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

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

        self.newSample.emit(randomSample)

classMyWindow(QtWidgets.QWidget):
    def__init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

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

        self.matplotlibWidget = MatplotlibWidget(self)

        self.layoutVertical = QtWidgets.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()defon_pushButtonPlot_clicked(self):
        self.samples = 0
        self.matplotlibWidget.axis.clear()
        self.threadSample.start()

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

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

if __name__ == "__main__":
    import sys

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

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

    sys.exit(app.exec_())

Solution 2:

Eli Bendersky has written a code example that uses matplotlib within PyQt: http://eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/

Solution 3:

Integrating Matplotlib with PyQt takes a little work. Here's an example: http://sourceforge.net/mailarchive/message.php?msg_id=29086544

However, there are a few plotting libraries designed specifically around PyQt:

Solution 4:

If I understand you correctly you have an application with a GUI and you want to plot a graph in a separate window than the GUI uses. pyqtgraph can do this nicely.

first type pip install pyqtgraph in the command prompt to install pyqtgraph

then

import pyqtgraph as pg 

    pg.setConfigOption('background', 'w')      # sets background to white                                                 
    pg.setConfigOption('foreground', 'k')      # sets axis color to black

    pw = pg.plot(x, y, pen='g')                # 1st plot (green)                                           
    pw.plot(x2, y2, pen='b')                   # 2nd plot in same figure (blue)

    pw.setLabel('bottom', 'x-label')           # x-label
    pw.setLabel('left', 'y-label')             # y-label

more info here: http://www.pyqtgraph.org/documentation/how_to_use.html

Post a Comment for "How To Plot On My Gui"