Paulus
Paulus

Reputation: 1457

How to insert a 3d GLViewWidget into a window containing 2d PyQtGraph plots

I am trying to port some plotting code from matplotlib to pyqtgraph for performance improvement. The pyqtgraph examples give separate examples of 2d plots and 3d plots, but I have not been able to find a sample of a 3d plot embedded as a subplot in a 2d plot window. I have tried embedding a GLViewWidget into a GraphicsWindow as shown below. What happens is that I can get multiple 2d plots or multiple 3d GLViewWidgets embedded, but not both together. As soon as I add 2d widgets to the layout, they dominate completely and I cannot see the 3d widgets.

I have also tried adding a QGroupBox widget, with its own layout, and adding the 3d widget to that, even trying to set the minimum Width of the group box and the 3d widget, none of those seemed to help.

I am using 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_()

Upvotes: 2

Views: 4043

Answers (1)

Josh.F
Josh.F

Reputation: 3806

"the PlotWidget has more aggressive default settings because it inherits from QGraphicsView" - source I have yet to understand PyQT(Graph) and OpenGL, so I'm sorry I can't say much more, but these 3 lines should solve your motivating example:

p1.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.sizeHint = lambda: pg.QtCore.QSize(100, 100)
glvw.setSizePolicy(p1.sizePolicy())

As in:

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

Upvotes: 3

Related Questions