问题
At the moment I am using matplotlib
to plot multiple numpy
arrays (or lists) of data. These correspond to approximately 3000 plots. The plots are time series.
My problem is that when these lists become too large with a lot of data, matplotlib is very slow. I read that pyqtgraph
can plot really fast, but my question is:
Can you use pyqtgraph to plot data without spawning that pyqt
window and instead directly save them to files? Matplotlib had an option to change backend so it does not spawn the window but instead just create the file, is there something similar for pyqtgraph?
Also do you think that, for my problem, pyqtgraph is the correct way to go? I see it a lot for plotting streaming data live, but what I want to do does not involve live stream data. Should I use something else to create plots for all my data?
回答1:
You do not need to show a window if you want to save an image of the plot, the following in an example:
import pyqtgraph as pg
import pyqtgraph.exporters
plt = pg.plot([1, 2, 3, 4, 5], [2, 5, 2, 5, 2])
exporter = pg.exporters.ImageExporter(plt.plotItem)
exporter.parameters()['width'] = 640
exporter.export('fileName.png')
Although that library has a bug, the solution is simple, you must go to the file pyqtgraph/exporters/ImageExporter.py
pyqtgraph
|
...
├── exporters
│ ├── ...
│ ├── ImageExporter.py
... ...
and change line 70 of:
bg = np.empty((self.params['width'], self.params['height'], 4), dtype=np.ubyte)
to:
bg = np.empty((int(self.params['width']), int(self.params['height']), 4), dtype=np.ubyte)
fileName.png
References:
- http://www.pyqtgraph.org/documentation/exporting.html
回答2:
To close the window that opens by default, you could add plt.win.close()
as seen below:
import pyqtgraph as pg
import pyqtgraph.exporters
plt = pg.plot([1, 4, 3, 9, 5], [2, 3, 2, 1, 0])
plt.win.close()
exporter = pg.exporters.ImageExporter(plt.plotItem)
exporter.parameters()['width'] = 640
exporter.export('fileName.png')
References: http://www.pyqtgraph.org/documentation/exporting.html
来源:https://stackoverflow.com/questions/48025999/pyqtgraph-for-plotting-multiple-data-lists