I am trying to plot live data using PyQtgraph
, which i started to learn. I read a lot of posts here, such as:
and searched for the docs in the internet. The problem that i`m having is that the plot is very slow while drawing the data. This is because, when the data comes from a serial port, I append it to a list, and then I take it from that list and draw it. So, the plot keeps drawing even after I shut down the device where the data is coming from.
This is my code:
class LivePlot(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.t = QTime()
self.t.start()
self.data = deque()
self.plot = self.win.addPlot(title='Timed data')
self.curve = self.plot.plot()
print "Opening port"
self.raw=serial.Serial("com4",115200)
print "Port is open"
self.tmr = QTimer()
self.tmr.timeout.connect(self.update)
self.tmr.start(100)
self.cnt = 0
def update(self):
line = self.raw.read()
ardString = map(ord, line)
for number in ardString:
numb = float(number/77.57)
self.cnt += 1
x = self.cnt/2
self.data.append({'x': x , 'y': numb})
x = [item['x'] for item in self.data]
y = [item['y'] for item in self.data]
self.curve.setData(x=x, y=y)
So, how can I draw the data without append it to a list? I am new with this library so i am confused. Hope you can help me.
---- EDIT ----
I've tried this modification in the code:
class LivePlot(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.cnt = 0
#Added this new lists
self.xData = []
self.yData = []
def update(self):
line = self.raw.read()
ardString = map(ord, line)
for number in ardString:
numb = float(number/77.57)
self.cnt += 1
x = self.cnt/2
self.data.append({'x': x , 'y': numb})
self.xData.append(x)
self.yData.append(numb)
self.curve.setData(x=self.xData, y=self.yData)
But i am having the same problem.