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
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
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_())
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.
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
@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.
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?