Python arduino和matplotlib pyqt上带有超声波传感器的实时图形显示图形
我坚持我的密码。我想在PyQt5上有GUI来显示我的图形。我在matplot中有工作代码,但无法将其转换为PyQT。我是这方面的初学者。我试图找到工作类似的代码,但我只是这样做。这在matplotlib上有效,但我想在GUI上使用它Python arduino和matplotlib pyqt上带有超声波传感器的实时图形显示图形,python,matplotlib,arduino,pyqt5,Python,Matplotlib,Arduino,Pyqt5,我坚持我的密码。我想在PyQt5上有GUI来显示我的图形。我在matplot中有工作代码,但无法将其转换为PyQT。我是这方面的初学者。我试图找到工作类似的代码,但我只是这样做。这在matplotlib上有效,但我想在GUI上使用它 import serial import numpy import matplotlib.pyplot as plt from drawnow import * distance =[] arduinoData=serial.Serial('COM4',11520
import serial
import numpy
import matplotlib.pyplot as plt
from drawnow import *
distance =[]
arduinoData=serial.Serial('COM4',115200)
plt.ion()
cnt=0
def makeFig():
plt.ylim(2,20)
plt.title('Plot ')
plt.grid(True)
plt.ylabel('Distance cm')
plt.plot(distance, 'ro-', label='Distance ')
plt.legend(loc='upper left')
while True: # While loop that loops forever
while (arduinoData.inWaiting()==0): #Wait here until there is data
pass #do nothing
arduinoString = arduinoData.readline() #read the line of text from the serial port
measurement = int(arduinoString) #Convert first element to floating number and put in measurement
distance.append(measurement) #Build our distance array by appending temp readings
drawnow(makeFig) #Call drawnow to update our live graph
plt.pause(.000001) #Pause Briefly. Important to keep drawnow from crashing
cnt=cnt+1
if(cnt>50): #If you have 50 or more points, delete the first one from the array
distance.pop(0) #This allows us to just see the last 50 data points
#This allows us to just see the last 50 data points
我的PyQt和Matplot。我想点击按钮并显示这个图表。
我在第51行的情节中发现了错误
测量=整数(arduinoString)
ValueError:以10为基数的int()的文本无效:b'\n''和
“后端TkAgg是交互式后端。正在打开交互式模式。”
我从我的传感器发送的数据是简单的距离,比如5,10等等
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import random
import serial
import numpy
from drawnow import *
arduinoData=serial.Serial('COM4',115200)
cnt=0
distance =[]
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def plot(self):
while True:
while (arduinoData.inWaiting()==0): #wait for data
pass
arduinoString=arduinoData.readline() #read the line of text from the serial port
measurement=int(arduinoString)
distance.append(measurement)
plt.pause(0.00001)
global cnt
cnt=cnt+1
if (cnt>50):
distance.pop(0)
self.figure.clear()
ax = self.figure.add_subplot(111)
ax.plot(distance, '*-')
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
这实际上并不是等待,它会执行低于它的任何内容,而不管您的条件检查如何。请按如下方式尝试:
while True:
if (arduinoData.inWaiting()==0):
continue
#rest of your code
现在我没有那个错误,但我仍然没有得到任何数据来绘图。它打开了一个新的matplotlib图形,而不是我的。你们可以看到我在这张图中的意思。我在def plot中勾选了最后4行,但我仍然有2行相同的plot,我如何删除其中一行?新版本负责显示更改,旧版本只是复制它,所以当我关闭新版本时,旧版本不起作用。
while True:
if (arduinoData.inWaiting()==0):
continue
#rest of your code