gpt4 book ai didi

python - 图表绘制速度比接收到的实时值慢

转载 作者:行者123 更新时间:2023-12-01 04:44:57 26 4
gpt4 key购买 nike

我正在尝试从 arduino 获取数据,然后尝试使用 subplot 在 python 中实时显示数据。来自 arduino uno 板的值速度很快,并且也以相同的速率显示在 python 控制台中,但是当我尝试在图表中绘制实时数据时,它的绘制速度非常慢。它需要与来自 uno 板的值的速率一样快。请帮忙。这是我的代码:

import serial 
import numpy
import matplotlib.pyplot as plt
from drawnow import *
x = []
y = []
z = []
magnitude = []
arduinoData = serial.Serial('com4', 9600)
plt.ion()
count=0
fig = plt.figure()

def makeFig():
ax1 = fig.add_subplot(4,1,1)
ax1.plot(x, 'ro-', label='X axis')
ax2 = fig.add_subplot(4,1,2)
ax2.plot(y, 'b^-', label='Y axis')
ax3 = fig.add_subplot(4,1,3)
ax3.plot(z, 'gp-', label='Y axis')
ax4 = fig.add_subplot(4,1,4)
ax4.plot(magnitude, 'yo-', label='X axis')
while True:
while (arduinoData.inWaiting()==0):
pass
arduinoString = arduinoData.readline()
dataArray = arduinoString.split(',')
xaxis = float( dataArray[0])
yaxis = float( dataArray[1])
zaxis = float( dataArray[2])
mag =float( dataArray[3])
x.append(xaxis)
y.append(yaxis)
z.append(zaxis)
magnitude.append(mag)
drawnow(makeFig)
count = count + 1

最佳答案

现在,在找到好的解决方案之前,您必须了解一些事情。数据从 arduino 到达的速度有多快? drawow 函数有多快?这些时间不受您的控制,因此如果数据到达的速度快于绘图例程的执行速度,则您定义的任务是不可能的。所有 Python 版本都有一个 time 模块,函数 time.time() 返回当前时间(以秒为单位)。这可以用来测量drawow函数的速度。在更新绘图之前,您可能需要缓存大量数据。每秒更新几次情节会给人一种实时的错觉,这可能就足够了。

要查看图表绘制的速度,请使用:

t = time.time()
drawnow()
print(time.time()-t) # time in seconds

关于python - 图表绘制速度比接收到的实时值慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com