Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyqt5和pyqtgraph构建两个图_Python_Pyqt_Pyqt5_Pyqtgraph - Fatal编程技术网

Python Pyqt5和pyqtgraph构建两个图

Python Pyqt5和pyqtgraph构建两个图,python,pyqt,pyqt5,pyqtgraph,Python,Pyqt,Pyqt5,Pyqtgraph,我正在尝试使用PyQt5和pyqtgraph框架构建一个应用程序。本质上,我试图在qmain窗口内的QWidget中放置两个图形。我现在正在进行单元测试,我很难使用PlotWidget、GraphicsWidow或GraphicsObject对图形进行编码。基本上是两个相同的玻璃杯,在第三节课上,它将集中在第四节课上。这就是我目前所拥有的 import sys from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication) imp

我正在尝试使用PyQt5和pyqtgraph框架构建一个应用程序。本质上,我试图在qmain窗口内的QWidget中放置两个图形。我现在正在进行单元测试,我很难使用PlotWidget、GraphicsWidow或GraphicsObject对图形进行编码。基本上是两个相同的玻璃杯,在第三节课上,它将集中在第四节课上。这就是我目前所拥有的

import sys
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication)
import pyqtgraph as pg

class CustomPlot(pg.GraphicsObject):
    def __init__(self):
        pg.GraphicsObject.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5
        self.y = self.x * 500 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='o', symbolPen='g', symbolSize=1))

# a class for the second plot to be displayed underneath the first via 
# QVBoxLayout

class CustomPlot1(pg.GraphicsObject):
    def __init__(self):
        pg.GraphicsObject.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5 #
        self.y = self.x * 750 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='t', symbolPen='g', symbolSize=1)

# The top container/widget for the graphs
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI() # call the UI set up

    # set up the UI
    def initUI(self):

        self.layout = QVBoxLayout(self) # create the layout
        self.guiplot = pg.PlotWidget() # create an instance of plotwidget 1
        self.guiplot1 = pg.PlotWidget() # create an instance of plotwidget 2
        self.pgcustom = CustomPlot() # class abstract both the classes
        self.pgcustom1 = CustomPlot1() # "" "" ""
        self.layout.addWidget(self.guiplot) # add the first plot widget to the layout
        self.guiplot.addItem(self.pgcustom) # now add the plotItem to the plot widget 
        self.layout.addWidget(self.guiplot1) # add the second plot widget to the layout


        self.guiplot1.addItem(self.pgcustom1)  # now add the plotItem to the plot widget 
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())


是否应该将PlotWidget改为GraphicsObject?

GraphicsObject没有
plot()
方法,您必须做的是从PlotWidget继承。每个类都有其功能,在您的情况下,您需要使用PlotWidget:

import sys
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication)
import pyqtgraph as pg
import numpy as np

class CustomPlot(pg.PlotWidget):
    def __init__(self):
        pg.PlotWidget.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5
        self.y = self.x * 500 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='o', symbolPen='g', symbolSize=1)

# a class for the second plot to be displayed underneath the first via 
# QVBoxLayout

class CustomPlot1(pg.PlotWidget):
    def __init__(self):
        pg.PlotWidget.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5 #
        self.y = self.x * 750 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='t', symbolPen='g', symbolSize=1)

# The top container/widget for the graphs
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI() # call the UI set up

    # set up the UI
    def initUI(self):

        self.layout = QVBoxLayout(self) # create the layout
        self.pgcustom = CustomPlot() # class abstract both the classes
        self.pgcustom1 = CustomPlot1() # "" "" ""
        self.layout.addWidget(self.pgcustom)
        self.layout.addWidget(self.pgcustom1)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())
输出:


GraphicsObject没有
plot()方法,您必须做的是从PlotWidget继承。每个类都有其功能,在您的情况下,您需要使用PlotWidget:

import sys
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QApplication)
import pyqtgraph as pg
import numpy as np

class CustomPlot(pg.PlotWidget):
    def __init__(self):
        pg.PlotWidget.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5
        self.y = self.x * 500 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='o', symbolPen='g', symbolSize=1)

# a class for the second plot to be displayed underneath the first via 
# QVBoxLayout

class CustomPlot1(pg.PlotWidget):
    def __init__(self):
        pg.PlotWidget.__init__(self)
        self.x = np.random.normal(size=1000) * 1e-5 #
        self.y = self.x * 750 + 0.005 * np.random.normal(size=1000)
        self.y -= self.y.min() - 1.0
        self.mask = self.x > 1e-15
        self.x = self.x[self.mask]
        self.y = self.y[self.mask]
        self.plot(self.x, self.y, pen='g', symbol='t', symbolPen='g', symbolSize=1)

# The top container/widget for the graphs
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI() # call the UI set up

    # set up the UI
    def initUI(self):

        self.layout = QVBoxLayout(self) # create the layout
        self.pgcustom = CustomPlot() # class abstract both the classes
        self.pgcustom1 = CustomPlot1() # "" "" ""
        self.layout.addWidget(self.pgcustom)
        self.layout.addWidget(self.pgcustom1)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())
输出:


@oOpSgEo如果我的答案对您有帮助,请不要忘记将其标记为正确,如果您不知道如何做,请查看,这是表示感谢的最佳方式:D@oOpSgEo如果我的答案对您有帮助,请不要忘记将其标记为正确,如果您不知道如何做,请查看,这是表示感谢的最佳方式D