Using PyQtGraph, I would like to generate three sub plots in one chart and export this chart to a file.
As I will repeat this a lot of times, it is quite performance sensitive. Therefore I do not need to bring the chart to the screen.
The code below shows the three subplots on the screen, but the export to the file is presently not working. What do I have to pass as parameter to ImageExporter
?
(and if possible: what do I have to do to just do the export without bringing the chart to the screen anyhow?)
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np
print("pyqtgraph.__version__:", pyqtgraph.__version__)
app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout(border=(100, 100, 100))
view.setCentralItem(l)
view.show()
view.resize(800, 600)
# l.addLayout(colspan=1, border=(50, 0, 0))
p1 = l.addPlot()
l.nextRow()
p2 = l.addPlot()
l.nextRow()
p3 = l.addPlot()
p1.hideAxis("left")
p1.hideAxis("bottom")
p2.hideAxis("left")
p2.hideAxis("bottom")
p3.hideAxis("left")
p3.hideAxis("bottom")
p1.plot([1, 3, 2, 4, 3, 5])
p2.plot([1, 3, 2, 4, 3, 5])
p3.plot([1, 3, 2, 4, 3, 5])
# exporter = pg.exporters.ImageExporter(plt.plotItem)
exporter = pg.exporters.ImageExporter(view.currentItem)
# set export parameters if needed
exporter.parameters()['width'] = 100 # (note this also affects height parameter)
# save to file
exporter.export('fileName.png')
results in:
pyqtgraph.__version__: 0.11.0.dev0+g9aaae8d
....
File "/home/user/PycharmProjects/0480_all_integrated/attic3.py", line 36, in <module>
exporter = pg.exporters.ImageExporter(view.currentItem)
File "/home/user/anaconda3/lib/python3.6/site-packages/pyqtgraph/exporters/ImageExporter.py", line 15, in __init__
tr = self.getTargetRect()
File "/home/user/anaconda3/lib/python3.6/site-packages/pyqtgraph/exporters/Exporter.py", line 96, in getTargetRect
return self.item.mapRectToDevice(self.item.boundingRect())
AttributeError: 'NoneType' object has no attribute 'mapRectToDevice'
The following small modification will work, observe the line where you define exporter:
I cross referenced my question on the PyQtGraph google forum and got a suitable workaround.