如何使用tkinter在python中绘制实时图形



此代码用于从arduino获取读数并在获取所有读数后制作图形。我想要实时绘图。

def takeRead():
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM7" # "COM#" this must match with connected port
ser.open() #opening serial port
while True:
global data
data = []
x = []
for j in range(8000):
line = ser.readline() # read a byte string
if line:
string = line.decode() # convert the byte string to a unicode string
res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
data.append(res) #append readings to store 
print(res)
x0= j*0.9
x.append(x0)
print(x0) 
#clear axes to clear the previous plot
#showing legend    ax.clear()
data = [val for sublist in data for val in sublist]
ax.plot(x,data,color='b',label='actual')
canvas.draw()
ax.legend()
break
print(data)
ser.close()

我尝试了很多东西,比如,把

ax.plot()
canvas.draw()

在我循环但滞后。下面是代码

def takeRead():
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM7" # "COM#" this must match with connected port
ser.open() #opening serial port
while True:
global data,x
data = []
x = []
for j in range(8000):
line = ser.readline() # read a byte string
if line:
string = line.decode() # convert the byte string to a unicode string
res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
data.append(res) #append readings to store 
print(res)
x0= j*0.9
x.append(x0)
print(x0)
ax.plot(x,data,color='b',label='actual')
canvas.draw()
#clear axes to clear the previous plot
#showing legend    ax.clear()

ax.legend()

break
ser.close()

also tried ' funanimation ' from 'matplotlib.animation'在这种情况下,图形仍然滞后,但在大约4000个读数时,它突然跳到零,然后跳过读数(可能是由于串行缓冲区的刷新或其他原因)

下面是FunctAnimation 的代码
def animate(i):
#print(z)
if(z==1):
#data_string=arduino.readline()
#print(data_string)
data=int(arduino.readline()) ## remove int() if you want string data to be represented
ys.append(data)
print(data)
ax.clear()
ax.plot([i for i in range(len(ys))],ys)  ## plots the graph
ax.set(xlabel='Time in x100 ms',ylabel='Magnitude')
ax.set_title('POT Readings')
else:
print("Not connected to com port")
anim = animation.FuncAnimation(fig, animate, interval=10)
plt.show()

有人建议我隔一段时间更新根窗口,但是我不知道该怎么做

这段代码是我的问题的一些解决方案,它仍然随机更新,有时它更新3次,有时甚至超过10次,但并不总是更新它应该在1000毫秒= 1秒后更新图形,但在一段时间后它停止更新

def takeRead():
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM7" # "COM#" this must match with connected port
ser.open() #opening serial port
while True:
global data,x
data = []
x = []
for j in range(8000):
line = ser.readline() # read a byte string
if line:
string = line.decode() # convert the byte string to a unicode string
res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
data.append(res) #append readings to store 
print(res)
x0= j*0.9
x.append(x0)
print(x0) 
#clear axes to clear the previous plot
#showing legend    ax.clear()
data = [val for sublist in data for val in sublist]
break
#threading.Thread.join()
#print(data)
ser.close()
def plotdata():
ax.plot(x,data,color='b',label='actual')
canvas.draw()
root.after(1000,plotdata)

最新更新