Alexis R Devitre
Alexis R Devitre

Reputation: 298

Best way to update a pyqtgraph?

I'm developing an app to collect and display experimental data, and control several devices. I'm doing the plotting with a Timer that pulls data from several arrays that are being filled continuously by other QTimer responsible for data collection.

After several hours of collecting and plotting at a rate slower than or equal to 1 point per second, the plotting becomes pretty slow and I don't think it's because I'm running out of memory (8 bytes x 12 hours x 3600 measurements/hour x 5 signals ~ 1.6 MB and I have at least 2GB of RAM) so I think there might be a lag on the re-plotting and clearing of the signals which also happens every second.

Currently I am using:

PlotItem.plot(x, y, clear=True) 

to update my plot. Is there a better way to do this? Some function that would add data points to the plot without repotting the whole thing. Having said that I'm going to go ahead and try:

PlotItem.plot(x[-2:-1], y[-2:-1], clear=False) 

But this might be a bit involved, as I would have to differentiate fresh and old data. Does anyone have a more elegant solution?

Upvotes: 0

Views: 2474

Answers (2)

Oded Ben Dov
Oded Ben Dov

Reputation: 10397

I built on top of the suggestions here, and added an essential piece: skipping out-of-sync signals.

I added a timestamp to each signal, and when time came to draw it, if it was 100ms stale, I'd drop it.

This way old frames dont get backlogged - you drop some frame along the way, but it doesn't matter becuase the newer signals contain them...

Hope this helps someone

Upvotes: 1

titusjan
titusjan

Reputation: 5546

In my experience plotting become noticeably slow at around 100000 points, so with 5 plots of 12*3600 points you reach this point.

You can't add data points incrementally to the graph because then the lines are discontinuous; you will have to replot every data point every time. However, it is possible to do this a bit more efficiently by using the setData method as explained my answer here.

Also disabling the axes auto-range will help a lot.

Finally, I noticed that plotting becomes really slow if you use anti-aliasing together with a line width larger than 1.0

Upvotes: 2

Related Questions