问题
I am trying to move from matplotlib to plotting with pyqtgraph because of its touted capabilities to render and save images faster. In my attempts to do this on a cluster with multiprocessors, I run into the following trouble:
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.
How do I obviate displaying a plot, and save it directly to file? Here's my attempt at the code:
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')
I have tested this on my personal laptop and it works fine.
回答1:
If you want to run a GUI without using desktop environment/window manager a possible solution is to use Xvfb.
来源:https://stackoverflow.com/questions/57315719/plotting-with-pyqtgraph-without-displaying