PyQtGraph plotting over time

Heads up! You've already completed this tutorial.

William_TD | 2020-05-07 20:38:24 UTC | #1

Hello Martin:

Just purchased your videos and book. I am looking for an example that plots amplitude over time in real time. That means that the plot scrolls across the X-axis continuously. Like an oscilloscope. Do you know of such an example?

Thank you,

William


martin | 2020-05-07 20:40:35 UTC | #2

Hey William, thanks for your comment. What you can do is take a reference from the first created plot and then call .setData() to update the data. I've updated the tutorial with an example for this. It uses random data, but it should make clear the principle. Let me know if you have any questions.

The example code is below

python
from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys  # We need sys so that we can pass argv to QApplication
import os
from random import randint

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)

        self.x = list(range(100))  # 100 time points
        self.y = [randint(0,100) for _ in range(100)]  # 100 data points

        self.graphWidget.setBackground('w')

        pen = pg.mkPen(color=(255, 0, 0))
        self.data_line =  self.graphWidget.plot(self.x, self.y, pen=pen)

        self.timer = QtCore.QTimer()
        self.timer.setInterval(50)
        self.timer.timeout.connect(self.update_plot_data)
        self.timer.start()

    def update_plot_data(self):

        self.x = self.x[1:]  # Remove the first y element.
        self.x.append(self.x[-1] + 1)  # Add a new value 1 higher than the last.

        self.y = self.y[1:]  # Remove the first 
        self.y.append( randint(0,100))  # Add a new random value.

        self.data_line.setData(self.x, self.y)  # Update the data.


app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Aqui_Carattino | 2020-05-07 20:41:51 UTC | #3

I know you are not using numpy on your code, but a very useful approach is to use np.roll() instead of appending/removing to a list. You can see a quick example here which will allow you to reach video rates even at high numbers of points being added to your data.


Hendrik_Nahler | 2020-05-07 20:58:08 UTC | #4

It would be indeed very helpful to see how the updating graph would work with 'external' data.

Could this be simulated by having a data source outside the MainWindow? - This could be generated (random e.g. in a np.array) in the main programme and then accessed with the updating time interval that you have set, taking the current value out of the np.array, possibly using the np.roll function mentioned by @Aqui_Carattino


Eolinwen | 2020-05-13 15:00:40 UTC | #5

@martin @William_TD

I'm in the same search for a futur big application that I will start in the next months and I will use a lot of Graphs (done with PyQgraph). And like you datas will come continually, (every 50 milliseconds) and I would like to be able to show them every 30 seconds, 5 minutes, 1 hour and one day. Can we do this easily ? And How ?

And I 'm going more away, be able to change the 50 milliseconds value by 10 or 100 milliseconds keeping in mind the wish to display in the graph datas every 30 seconds, 5 minutes, 1 hour and one day.

BTW Thanks.


erhughes4606 | 2020-06-05 19:01:06 UTC | #6

This new add-on gives me a typeerror: 'builtin_function_or_method' object is not subscriptable. I am running Python 3.8.1 and Windows 10. I had to use version 0.11.0rc0 to get rid of the time.clock error mentioned above. Then I had to change LabelItem.py and functions.py to permit it to take the color and size calls listed here (respectfully, color='blue' and size=30). So it was running properly until I included this add-on. The error comes on the first line of update_plot_data. Any ideas why I'm getting this error in the code?


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

PyQtGraph plotting over time was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.