如何将 3d GLViewWidget 插入包含 2d PyQtGraph 图的窗口中
Posted
技术标签:
【中文标题】如何将 3d GLViewWidget 插入包含 2d PyQtGraph 图的窗口中【英文标题】:How to insert a 3d GLViewWidget into a window containing 2d PyQtGraph plots 【发布时间】:2019-03-13 05:54:36 【问题描述】:我正在尝试将一些绘图代码从 matplotlib 移植到 pyqtgraph 以提高性能。 pyqtgraph 示例给出了 2d 图和 3d 图的单独示例,但我无法在 2d 绘图窗口中找到嵌入为子图的 3d 图样本。 我尝试将 GLViewWidget 嵌入到 GraphicsWindow 中,如下所示。发生的情况是我可以嵌入多个 2d 绘图或多个 3d GLViewWidgets,但不能同时嵌入。一旦我将 2d 小部件添加到布局中,它们就完全占主导地位,而我看不到 3d 小部件。
我还尝试添加一个 QGroupBox 小部件,具有自己的布局,并添加 3d 小部件,甚至尝试设置组框和 3d 小部件的最小宽度,这些似乎都没有帮助。
我正在使用 Anaconda 3:
pyqt 5.6.0 py37ha878b3d_6
pyqtgraph 0.10.0 py37h28b3542_3
python 3.7.0
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import pyqtgraph.opengl as gl
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="A 2d plot window")
p1 = pg.PlotWidget()
# try adding a 3d plot
glvw = gl.GLViewWidget()
z = pg.gaussianFilter(np.random.normal(size=(50,50)), (1,1))
p13d = gl.GLSurfacePlotItem(z=z, shader='shaded', color=(0.5, 0.5, 1, 1))
glvw.addItem(p13d)
# get a layout
layoutgb = QtGui.QGridLayout()
win.setLayout(layoutgb)
layoutgb.addWidget(glvw, 0, 0)
# layoutgb.addWidget(p1, 0, 1) ### uncommenting this line causes
#the plot widget to appear and the 3d widget to disappear
QtGui.QApplication.instance().exec_()
【问题讨论】:
【参考方案1】:“PlotWidget 具有更激进的默认设置,因为它继承自 QGraphicsView”-source 我还没有理解 PyQT(Graph) 和 OpenGL,所以很抱歉我不能多说,但是这 3 行应该解决你的激励例子:
p1.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.setSizePolicy(p1.sizePolicy())
如:
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import pyqtgraph.opengl as gl
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="A 2d plot window")
p1 = pg.PlotWidget()
# try adding a 3d plot
glvw = gl.GLViewWidget()
z = pg.gaussianFilter(np.random.normal(size=(50,50)), (1,1))
p13d = gl.GLSurfacePlotItem(z=z, shader='shaded', color=(0.5, 0.5, 1, 1))
glvw.addItem(p13d)
# get a layout
layoutgb = QtGui.QGridLayout()
win.setLayout(layoutgb)
layoutgb.addWidget(glvw, 0, 0)
layoutgb.addWidget(p1, 0, 1) ### uncommenting this line causes
# the plot widget to appear and the 3d widget to disappear
p1.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.setSizePolicy(p1.sizePolicy())
QtGui.QApplication.instance().exec_()
【讨论】:
谢谢乔希,很抱歉我花了这么长时间才回到这个问题......仅仅三年!但你的答案是正确的以上是关于如何将 3d GLViewWidget 插入包含 2d PyQtGraph 图的窗口中的主要内容,如果未能解决你的问题,请参考以下文章