Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqtgraph: save / export 3d graph

I have plotted a 3D graph with pyqtgraph, and I want to save / export it. A right mouse click on the 3D plot doesn't open any context menu which would allow me to save the plot. The doc at http://www.pyqtgraph.org/documentation/exporting.html tells me how to save / export from within a program but following the instructions for 3D results in a black saved image.

Here is the relevant portion of my code:

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np
import pyqtgraph.exporters

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.show()
w.setCameraPosition(distance=50)

g = gl.GLGridItem()
g.scale(2,2,1)
g.setDepthValue(10) 
w.addItem(g)

z=np.genfromtxt('../mydata.txt') 
p1 = gl.GLSurfacePlotItem(z=z, shader='shaded', color=(0.5, 0.5, 1, 1))

p1.scale(0.1, 0.1, 0.1)
p1.translate(-0, 0, 0)
w.addItem(p1)

w.grabFrameBuffer().save('test.png')

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

mydata.txt contains a 2D array of float values. The plot appears correctly on my screen. Has anyone been successful in saving / exporting a 3D plot from a pyqtgraph program or is able to spot an issue in the code above? (Linux, Using Qt version 4.8.7 in anaconda3).

like image 264
calocedrus Avatar asked Oct 19 '22 09:10

calocedrus


1 Answers

This is the relevant information which helped me to solve my problem: https://groups.google.com/forum/#!msg/pyqtgraph/dKT1Z3nIeow/OErAgRPAbB8J

That is:

d = w.renderToArray((1000, 1000))
pg.makeQImage(d).save(filename)

And here below is the full code which creates a 3D plot and saves it:

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np
import pyqtgraph.exporters

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.show()
w.setCameraPosition(distance=50)

g = gl.GLGridItem()
g.scale(2,2,1)
g.setDepthValue(10) 
w.addItem(g)

z=np.genfromtxt('../../TestData/textAsImage.txt') 
p1 = gl.GLSurfacePlotItem(z=z, shader='shaded', color=(0.5, 0.5, 1, 1))

p1.scale(0.1, 0.1, 0.1)
p1.translate(-0, 0, 0)
w.addItem(p1)

filename = 'yourfilename.png'
d = w.renderToArray((1000, 1000))
pg.makeQImage(d).save(filename)

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
like image 195
calocedrus Avatar answered Oct 21 '22 05:10

calocedrus