使用pyqtgraph绘图而不显示

Plotting with pyqtgraph without displaying

我正在尝试从 matplotlib 转向使用 pyqtgraph 绘图,因为它被吹捧为能够更快地渲染和保存图像。在我尝试在具有多处理器的集群上执行此操作时,我 运行 遇到了以下问题:

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-user' qt.qpa.screen: QXcbConnection: Could not connect to display Could not connect to any X display.

如何避免显示绘图并将其直接保存到文件?这是我对代码的尝试:

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.exporters

#app = QtGui.QApplication([])
#view = pg.GraphicsView()
l = pg.GraphicsWindow()
#view.setCentralItem(l)
#view.show()
#view.setWindowTitle('GraphicsLayout')
#view.resize(1000,1600)


def plotlayout(lines):
    p_res={}
    p_data={}
    for rows in rows:
        p_res[row]={}
        p_data[row]={}       
        for col in cols:
            l2=l.addLayout()
            p_res[row][col]=l2.addPlot()
            p_res[row][col].hideAxis('bottom')
            l2.nextRow()
            p_data[row][col]=l2.addPlot()
            l.nextColumn()
        l.nextRow()
    return p_res, p_data    
pl = plotlayout(lines) 
pl[0].plot([1,3,5,9,7,8],[2,3,3,5,6,8])                
pl[1].plot([1,3,5,9,7,8],[2,22,3,45,6,8])
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
        QtGui.QApplication.exit()
        QtGui.QApplication.quit()
        QtGui.QApplication.quitOnLastWindowClosed()
        QtGui.QApplication.closeAllWindows()
exporter = pg.exporters.ImageExporter(l.scene())
exporter.export('fits.ps')

我已经在我的个人笔记本电脑上测试过了,它工作正常。

如果您想 运行 GUI 不使用桌面 environment/window 管理器,可能的解决方案是使用 Xvfb