【发布时间】:2016-09-01 23:15:14
【问题描述】:
我正在尝试使用我开始学习的PyQtgraph 绘制实时数据。我在这里看了很多帖子,比如:
并在互联网上搜索文档。我遇到的问题是绘制数据时绘图非常慢。这是因为,当数据来自串行端口时,我将其附加到一个列表中,然后从该列表中取出并绘制它。因此,即使在我关闭了数据来源的设备后,绘图仍会继续绘制。
这是我的代码:
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)
那么,如何在不将数据附加到列表的情况下绘制数据?我是这个图书馆的新手,所以我很困惑。希望你能帮助我。
---- 编辑----
我已经在代码中尝试过这种修改:
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)
但是我遇到了同样的问题。
【问题讨论】:
标签: python serial-port pyqtgraph