Created
November 22, 2018 07:08
-
-
Save nyanpasu64/a5d041032ba9244c0a67eeb8455e923d to your computer and use it in GitHub Desktop.
pyqtgraph plot 600 times... qimage is much slower
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Update a simple plot as rapidly as possible to measure speed. | |
| """ | |
| ## Add path to library (just for examples; you do not need this) | |
| from pyqtgraph.Qt import QtGui, QtCore | |
| import numpy as np | |
| import pyqtgraph as pg | |
| from pyqtgraph.ptime import time | |
| app = QtGui.QApplication([]) | |
| dims = 1280, 720 | |
| nsamp = 1102 # 25ms of 44100smp/s, subsampling=1 | |
| ndat = 50 | |
| data = np.random.normal(size=(ndat, nsamp)) | |
| p = pg.plot() | |
| p.setWindowTitle('pyqtgraph example: PlotSpeedTest') | |
| p.setRange(QtCore.QRectF(0, -10, nsamp-1, 20), padding=0) | |
| # p.setLabel('bottom', 'Index', units='B') | |
| p.resize(*dims) | |
| p.centralWidget.resize(*dims) | |
| p.plotItem.hideAxis('left') | |
| p.plotItem.hideAxis('bottom') | |
| curve = p.plot() | |
| # curve.setFillBrush((0, 0, 100, 100)) | |
| # curve.setFillLevel(0) | |
| # lr = pg.LinearRegionItem([100, 4900]) | |
| # p.addItem(lr) | |
| ptr = 0 | |
| lastTime = time() | |
| fps = None | |
| def update(): | |
| global curve, data, ptr, p, lastTime, fps | |
| if ptr == 600: | |
| QtCore.QCoreApplication.quit() | |
| curve.setData(data[ptr % ndat]) | |
| ptr += 1 | |
| now = time() | |
| dt = now - lastTime | |
| lastTime = now | |
| if fps is None: | |
| fps = 1.0 / dt | |
| else: | |
| s = np.clip(dt * 3., 0, 1) | |
| fps = fps * (1 - s) + (1.0 / dt) * s | |
| print('%0.2f fps' % fps) | |
| app.processEvents() ## force complete redraw for every plot | |
| timer = QtCore.QTimer() | |
| timer.timeout.connect(update) | |
| timer.start(0) | |
| ## Start Qt event loop unless running in interactive mode. | |
| if __name__ == '__main__': | |
| import sys | |
| if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | |
| QtGui.QApplication.instance().exec_() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Update a simple plot as rapidly as possible to measure speed. | |
| """ | |
| ## Add path to library (just for examples; you do not need this) | |
| from pyqtgraph.Qt import QtGui, QtCore | |
| import numpy as np | |
| import pyqtgraph as pg | |
| import pyqtgraph.exporters | |
| from pyqtgraph.ptime import time | |
| app = QtGui.QApplication([]) | |
| dims = 1280, 720 | |
| nsamp = 1102 # 25ms of 44100smp/s, subsampling=1 | |
| ndat = 50 | |
| data = np.random.normal(size=(ndat, nsamp)) | |
| p = pg.PlotWidget() | |
| p.setWindowTitle('pyqtgraph example: PlotSpeedTest') | |
| p.setRange(QtCore.QRectF(0, -10, nsamp-1, 20), padding=0) | |
| # p.setLabel('bottom', 'Index', units='B') | |
| p.resize(*dims) | |
| p.centralWidget.resize(*dims) | |
| p.plotItem.hideAxis('left') | |
| p.plotItem.hideAxis('bottom') | |
| curve = p.plot() | |
| # curve.setFillBrush((0, 0, 100, 100)) | |
| # curve.setFillLevel(0) | |
| # lr = pg.LinearRegionItem([100, 4900]) | |
| # p.addItem(lr) | |
| ptr = 0 | |
| lastTime = time() | |
| fps = None | |
| def update(): | |
| global curve, data, ptr, p, lastTime, fps | |
| curve.setData(data[ptr % ndat]) | |
| ptr += 1 | |
| now = time() | |
| dt = now - lastTime | |
| lastTime = now | |
| if fps is None: | |
| fps = 1.0 / dt | |
| else: | |
| s = np.clip(dt * 3., 0, 1) | |
| fps = fps * (1 - s) + (1.0 / dt) * s | |
| print('%0.2f fps' % fps) | |
| app.processEvents() ## force complete redraw for every plot | |
| class MyExporter(pg.exporters.ImageExporter): | |
| def __init__(self, item: pg.GraphicsWidget): | |
| super().__init__(item) | |
| # noinspection PyMethodOverriding | |
| def export(self) -> 'QtGui.QImage': | |
| QtGui.QApplication.processEvents() | |
| return super().export(toBytes=True) | |
| e = MyExporter(p.centralWidget) | |
| for i in range(600): | |
| # QApplication.processEvents() is called twice. | |
| # removing 1 or both calls does not affect performance. | |
| update() | |
| e.export() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment