用 pyqtgraph 绘图而不显示 [英] Plotting with pyqtgraph without displaying
问题描述
我正试图从 matplotlib 转向使用 pyqtgraph 绘图,因为它吹捧了可以更快地渲染和保存图像的能力.在尝试在具有多处理器的集群上执行此操作时,我遇到了以下问题:
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 未设置,默认为'/tmp/runtime-user' qt.qpa.screen: QXcbConnection: 无法连接to display 无法连接到任何 X 显示器.
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.
推荐答案
如果您想在不使用桌面环境/窗口管理器的情况下运行 GUI,一个可能的解决方案是使用 Xvfb.
If you want to run a GUI without using desktop environment/window manager a possible solution is to use Xvfb.
这篇关于用 pyqtgraph 绘图而不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!