gpt4 book ai didi

python - 如何将 3d GLViewWidget 插入包含 2d PyQtGraph 绘图的窗口中

转载 作者:太空宇宙 更新时间:2023-11-03 21:42:57 27 4
gpt4 key购买 nike

我正在尝试将一些绘图代码从 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_()

最佳答案

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

关于python - 如何将 3d GLViewWidget 插入包含 2d PyQtGraph 绘图的窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52704327/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com