pyqtgraph:保存/导出3d图形

时间:2016-05-20 03:02:38

标签: python qt 3d pyqtgraph

我用pyqtgraph绘制了一个3D图形,我想保存/导出它。 在3D绘图上单击鼠标右键不会打开任何允许我保存绘图的上下文菜单。 http://www.pyqtgraph.org/documentation/exporting.html上的文档告诉我如何在程序中保存/导出,但遵循黑色保存图像中3D结果的说明。

以下是我的代码的相关部分:

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包含一个浮点值的二维数组。该图在我的屏幕上正确显示。 有没有人成功地从pyqtgraph程序中保存/导出3D绘图,或者能够在上面的代码中发现问题? (Linux,在anaconda3中使用Qt版本4.8.7)。

1 个答案:

答案 0 :(得分:3)

这是帮助我解决问题的相关信息: https://groups.google.com/forum/#!msg/pyqtgraph/dKT1Z3nIeow/OErAgRPAbB8J

那是:

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

以下是创建3D图并保存的完整代码:

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