python - Pyqt5 与 pyqtgraph 构建两个图
问题描述
我正在尝试使用 PyQt5 和 pyqtgraph 框架构建应用程序。本质上,我试图将两个图形放在 QMainwindow 内的 QWidget 中。我现在正在进行单元测试,并且很难使用 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_())
我应该将 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_())
输出:
推荐阅读
- python - 在比较两个列表之前排序
- python - 我想向 QVBoxLayout 添加滚动条
- python - 如何使用熊猫在 jupyter 单元格中显示多个表格/图表?
- javascript - electron-vue:存储属性集但不渲染(VUEX)
- php - 多选下拉全选
- c# - 尝试在自己的 using 语句中访问 MemoryStream 时抛出 ObjectDisposedException
- java - 如何找出可以在 PDF 中以给定字体呈现的字符?
- javascript - TypeError:无法读取未定义的属性“孩子”
- java - 我无法将 th:object 传递给控制器并使用 @ModelAttribute 映射到 pojo
- css - 如何在 div 的样式属性中引用背景图像?