python - Performance issues with pyqtgraph -
i'm trying convert code using matplotlib pyqtgraph since it's supposed more efficient, , provides lot more interactive features. i've got code converted, i'm running issues runs slowly. code reproduces this:
import numpy np import qtpy.qtwidgets qt import pyqtgraph pg class graphwidget(qt.qwidget): """a widget simplifying graphing tasks :param qt.qwidget parent: :param dict[str, dict] layout: mapping title row/col/rowspan/colspan kwargs """ def __init__(self, parent, layout_spec): super(graphwidget, self).__init__(parent=parent) self.axes = {} glw = pg.graphicslayoutwidget(parent=self) name, layout in layout_spec.items(): self.axes[name] = pg.plotitem(name=name, title=name) glw.additem(self.axes[name], **layout) box_layout = qt.qvboxlayout() box_layout.addwidget(glw, 1) self.setlayout(box_layout) @property def normal_pen(self): return pg.mkpen(color='w', width=2) @property def good_pen(self): return pg.mkpen(color='g', width=2) @property def bad_pen(self): return pg.mkpen(color='r', width=2) def plot(self, mode, x, y, axis): if mode == 'normal': pen = self.normal_pen elif mode == 'good': pen = self.good_pen elif mode == 'bad': pen = self.bad_pen plot_item = pg.plotcurveitem(x, y, pen=pen) self.axes[axis].additem(plot_item) if __name__ == '__main__': import random import time # qt.qapplication.setgraphicssystem('opengl') app = qt.qapplication([]) window = qt.qwidget(parent=none) layout = qt.qvboxlayout() gw = graphwidget( window, { 'a': dict(row=1, col=1, rowspan=2), 'b': dict(row=1, col=2), 'c': dict(row=2, col=2), 'd': dict(row=1, col=3), 'e': dict(row=2, col=3), 'f': dict(row=1, col=4), 'g': dict(row=2, col=4), } ) layout.addwidget(gw, 1) def plot(): start = time.time() axis in 'abcdefg': gw.plot( random.choice(['normal', 'good', 'bad']), np.arange(2000), np.random.rand(2000), axis, ) # necessary because without it, "plotting" completes in ms, # ui doesn't update while still app.processevents() print('plotting time: {}'.format(time.time() - start)) button = qt.qpushbutton(parent=window, text='plot') button.pressed.connect(plot) layout.addwidget(button) window.setlayout(layout) window.showmaximized() app.exec_()
the number , layout of plots, , number of points, chosen reflect real-world use case have. if run , click plot button twice see
plotting time: 3.61599993706 plotting time: 7.04699993134
i stopped @ point because application in general slow , took several seconds close. if uncomment 1 line enable opengl rendering, can run more times , looks like
plotting time: 0.0520000457764 plotting time: 0.328999996185 plotting time: 0.453000068665 plotting time: 0.55999994278 plotting time: 0.674000024796 plotting time: 1.21900010109 plotting time: 0.936000108719 plotting time: 1.06100010872 plotting time: 1.19899988174 plotting time: 1.35100007057
at point can tell times reported here not accurate, takes longer these times ui reflect update.
this typical number of graphs me able deal with, application see 16 sets of graphs (one additional line per axis) in 20 seconds. if ui getting progressively slower isn't sufficiently fast.
down-sampling doesn't seem apply plotcurveitem (unlike plotdataitem), did discover doing
plot_item = pg.plotcurveitem(x, y, pen=pen, connect='pairs')
results in faster times:
plotting time: 0.0520000457764 plotting time: 0.0900001525879 plotting time: 0.138000011444 plotting time: 0.108000040054 plotting time: 0.117000102997 plotting time: 0.12299990654 plotting time: 0.143000125885 plotting time: 0.15499997139
this still seems pretty slow me though, , i'm wondering if there's can done speed further. it's still slow (on order of several seconds per plot) if don't set opengl. i'm looking fast of plotting can possibly manage. target <100ms per plot.
it's hard tell slowdowns though, profiling code seems difficult since of happens @ opengl level, deep inside qt code, , in pyqtgraph itself.
is there other way speed code further?
note: using python 2.7 64bit, pyqt 4.10.4, , pyqtgraph 0.10.0 installed via conda, although code need work equally on python 3.5+.
Comments
Post a Comment