Matplotlib animate plot - 图在循环完成之前没有响应

Matplotlib animate plot - Figure not responding until loop is done

我正在尝试为我的两个向量 X、Y 通过循环更新的情节制作动画。 我正在使用 FuncAnimation。我 运行 遇到的问题是,在循环完成之前,图形会显示 Not Responding 或空白。

所以在循环中,我会得到类似的东西:

但如果我停止循环或在结束时,图形就会出现。

我已将 图形后端 设置为 automatic

代码示例如下:

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

def animate( intermediate_values):
    x = [i for i in range(len(intermediate_values))]
    y = intermediate_values 
    plt.cla()
    plt.plot(x,y, label = '...')
    plt.legend(loc = 'upper left')
    plt.tight_layout()        
    
    
x = []
y = []
#plt.ion()
for i in range(50):
    x.append(i)
    y.append(i)
    ani = FuncAnimation(plt.gcf(), animate(y), interval = 50)  
    plt.tight_layout()
    #plt.ioff()
    plt.show()     

matplotlib中动画的结构是循环过程中不使用动画函数,而是动画函数是循环过程。设置初始图形后,动画功能将更新数据。

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

x = []
y = []

fig = plt.figure()
ax = plt.axes(xlim=(0,50), ylim=(0, 50))
line, = ax.plot([], [], 'b-', lw=3, label='...')
ax.legend(loc='upper left')


def animate(i):
    x.append(i)
    y.append(i)
    line.set_data(x, y)
    return line,

ani = FuncAnimation(fig, animate, frames=50, interval=50, repeat=False)

plt.show()