简体   繁体   English

如何在我的GUI上绘图

[英]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. 我正在设计一个使用PyQt的GUI,当我点击一个按钮时,我需要显示一个matplotlib / pylab窗口,该按钮用于创建我创建的函数的数据图。 It's like a runtime used in Matlab. 这就像在Matlab中使用的运行时。 I want to keep the matplotlib/pylab window as my window everytime I press that button. 每当我按下那个按钮时,我想保持matplotlib / pylab窗口作为我的窗口。

Here is a basic example that will plot three different samples using a QThread : 以下是使用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_())

图片

This is code from user1006989 (best answer) adapted to PyQt5 , hopefully it will be useful to someone: 这是来自user1006989(最佳答案)的代码,适用于PyQt5 ,希望它对某人有用:

Here is a basic example that will plot three different samples using a QThread: 以下是使用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 pyqt
from PyQt5 import QtWidgets

class MatplotlibWidget(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)

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(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()
    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 = QtWidgets.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

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

    sys.exit(app.exec_())

Eli Bendersky编写了一个在PyQt中使用matplotlib的代码示例: http//eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/

Integrating Matplotlib with PyQt takes a little work. 将Matplotlib与PyQt集成需要一些工作。 Here's an example: http://sourceforge.net/mailarchive/message.php?msg_id=29086544 这是一个例子: http//sourceforge.net/mailarchive/message.php?msg_id = 29086544

However, there are a few plotting libraries designed specifically around PyQt: 但是,有一些专门围绕PyQt设计的绘图库:

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. 如果我理解正确,你有一个带GUI的应用程序,你想在一个单独的窗口中绘制一个图形而不是GUI使用。 pyqtgraph can do this nicely. pyqtgraph可以做得很好。

first type pip install pyqtgraph in the command prompt to install pyqtgraph 首先在命令提示符下输入pip install pyqtgraph来安装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 更多信息: http//www.pyqtgraph.org/documentation/how_to_use.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM