Python> Matplotlib>动画现场情节>帧率

时间:2017-12-26 22:41:40

标签: python matplotlib

这是我的第一个问题帖子。

我使用下面的代码在我的实时情节中看到每5秒刷新一次约1帧。我想看到接近实时更新的东西。绘制的数据的串行速率足以支持每秒至少2帧。

我使用我在本网站上找到的示例构建了这个简单的脚本,并阅读了几个与重新应用这些示例相关的问题。

我不是经常/有经验的编码员。

import serial #import pyserial library

ser=serial.Serial('/dev/tty.usbmodem14611', baudrate = 9600, timeout=1) #set serial COM port, baud, [timeout if you are using a while loop]
my_list = []    # define a list to hold each data point as it comes in
count_list = [] # define a list to hold the x axis data point (in our case we will grab the computer time stamp of each data point)

import matplotlib.pyplot as plt #import the matplot library
import matplotlib.animation as animation #define the matplotlib animation function object
import time #import the python time library

fig = plt.figure() #define a figure object
ax1 = fig.add_subplot(1,1,1) #add a subplot to the figure, you can add more subplots for additional data types if desired

xar = [] #define a list to hold the new x dimension of plot on each update
yar = [] #define a list to hold the new y dimension of plot on each update

def animate(i): #define an animation function 

    arduinoData = ser.readline().decode('ascii') #read current data on serial monitor, decode as ascii data type
    Data = arduinoData.split() #split each word/text block into a temporary list named Data
    if len(Data) > 2: #check if Data has at least 3 words/text blocks
        if Data[0]=='Yaw,': #check if position 0 in the temporary list Data is equal to "Yaw,"

            p=Data[3] #set variable p equal to position 3 of the list Data
            s = p.replace(',', '') #set variable s equal to variable p, after removing the comma
            yar.append(float(s))
            print(float(s))

            #millis = int(round(time.time() * 1000)) #grab current EPOCH time of loop from local pc  
            xtime = time.time()  
            xar.append(xtime)



    ax1.clear() #clear previous axis data
    ax1.plot(xar,yar) #plot updated axis data for xar and yar lists

ani = animation.FuncAnimation(fig, animate, interval=1) #call the animation function with an interval of ? millis
plt.show() #show plot in gui

1 个答案:

答案 0 :(得分:0)

我找到了自己的答案@

https://stackoverflow.com/a/40749129

这总体上解决了这个问题。