Pyqt5 с pyqtgraph строит два графика - PullRequest
0 голосов
/ 28 апреля 2018

Я пытаюсь создать приложение, используя PyQt5 и pyqtgraph Framework. По сути, я пытаюсь поместить два графика в QWidget в QMainwindow. Я сейчас занимаюсь модульным тестированием, и мне трудно кодировать графики с помощью PlotWidget, GraphicsWindow или 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_())

enter image description here

enter image description here

Должен ли я использовать класс PlotWidget вместо GraphicsObject?

1 Ответ

0 голосов
/ 28 апреля 2018

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_())

Выход:

enter image description here

...