使用 Matplotlib FuncAnimation 绘制信号数据和缺少信息的跳帧

Plot signal data and skip frame with missing information using Matplotlib FuncAnimation

我正在模仿获取数据流,其中不是每个“消息”行都有我想在实时模式下绘制的信息。由于仍将读取每一行,我如何才能跳过一个迭代(帧)并因此跳过 1 帧的绘图更新,直到 returns 数据出现的下一次迭代。我的代码具有以下结构:

fig, ax = plt.subplots()
line, = ax.plot([], [])

lap_stat = []
file = open(path + 'file.log', 'r')

def update(lap_stat):

    #Plot lap number on x-axis, lap time on y-axis from lap_stat list that 
    #contains a tuple

    line.set_data(lap_stat[0], lap_stat[1])
    return line,
    
def data_gen():

    while True:
        line = file.readline()

        #Get data from line
        #Apply conditions, if line doesn't contain relevant information nothing happens
        #If data is present in line, save data as tuple [lap, lap_time] to lap_stat

        yield lap_stat

ani = FuncAnimation(fig, update, data_gen, interval=100, blit=True)
plt.show() 

并非每一行都有相关信息,因此如果在当前帧没有附加信息,更新(lap_stat)函数的输入列表为空,则不会绘制任何内容并且进程会终止。如何修改我的代码以仅在返回时绘制信息?绘制 0 也不是一个选项,如果我绘制第 0-10 圈的单圈时间,然后下一个数据点是第 15 圈,我希望第 10 圈的点连接到第 15 点。希望它成功,谢谢!

你让我汗流浃背,我认为这不是解决问题的正确方法,但考虑到你的布局,我的工作如下:

输入文件file.log:

1 10
2 20
3 10
4 50
5 80
6 10
7 70
8
9
10 55
11 40
12 66

我的代码:

import matplotlib.animation as animation #to save animation

from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.set_xlim([0,12])
ax.set_ylim([0,100])

line, = ax.plot([],[], marker="o", markersize=10,markeredgecolor="red", markerfacecolor="green")

path = ''

file = open(path + 'file.log', 'r')


def zero():
    line = ax.plot(0,0)
    return line 


def update(lap_stat):
    #Plot lap number on x-axis, lap time on y-axis from lap_stat list that 
    #contains a tuple
    line.set_data(lap_stat[0], lap_stat[1])
    return line,

    
def data_gen():
    line = 'pippo'
    
    while line:
        line = file.readline()
        print('line : ', line)
        
        try:
                lap_stat = (int(line.split()[0]),int( line.split()[1]))
                print('lap_stat : ', lap_stat)    
            #Get data from line
            #Apply conditions, if line doesn't contain relevant information nothing happens
            #If data is present in line, save data as tuple [lap, lap_time] to lap_stat
                yield lap_stat
                
        except:
                continue
        
ani = FuncAnimation(fig, update, data_gen(), init_func=zero ,  interval=2000, blit=True)
# ani = FuncAnimation(fig, update, data_gen(), interval=1000) # need first frame, doesnt clear previous points
# ani.save('animation.gif' , writer=animation.PillowWriter(fps=1)) #save animation

plt.show() 


输出:

根据 OP,修改相同输入的代码以获得以下动画:

import matplotlib.animation as animation #to save animation

from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt



fig, ax = plt.subplots()

ax.set_xlim([0,1])
ax.set_ylim([0,1])

line, = ax.plot([],[], marker="o", markersize=10,markeredgecolor="red", markerfacecolor="green")

path = ''

file = open(path + 'file.log', 'r')


def zero():
    line = ax.plot(0,0)
    return line 

x_points = [0]
y_points = [0]
def update(lap_stat):
    #Plot lap number on x-axis, lap time on y-axis from lap_stat list that 
    #contains a tuple
    
    x_points.append(lap_stat[0])
    
    y_points.append(lap_stat[1])
    
    ax.set_xlim([0,max(x_points)])
    ax.set_ylim([0,max(y_points)])
    
    # line.set_data(lap_stat[0], lap_stat[1])
    
    line.set_data(x_points, y_points)
    
    print(x_points, y_points)
    
    return line,

    
def data_gen():
    line = 'pippo'
    
    while line:
        line = file.readline()
        print('line : ', line)
        
        try:
                lap_stat = (int(line.split()[0]),int( line.split()[1]))
                print('lap_stat : ', lap_stat)    
            #Get data from line
            #Apply conditions, if line doesn't contain relevant information nothing happens
            #If data is present in line, save data as tuple [lap, lap_time] to lap_stat
                yield lap_stat
                
        except:
                continue
        
# ani = FuncAnimation(fig, update, data_gen(), init_func=zero ,  interval=2000, blit=True)

ani = FuncAnimation(fig, update, data_gen(), init_func=zero ,  interval=2000, blit=False, repeat = False)

# ani = FuncAnimation(fig, update, data_gen(), interval=1000) # need first frame, doesnt clear previous points
# ani.save('animation_line.gif' , writer=animation.PillowWriter(fps=1)) #save animation

plt.show() 

print(x_points, y_points)

输出: