如何使用 Matplotlib 的动画功能?

How to use Matplotlib's animate function?

谁能看出我的代码到底出了什么问题?我正在尝试生成我的结果的动画图以查看其随时间的演变,结果只是我的数据的完整图,而不是更新动画。

sampleText.txt 文件只是一个大小为 (5000, 1) 的 numpy 数组,数组的前 5 个条目是 [[-0.01955058],[ 0.00658392[,[-0.00658371],[-0.0061325 ],[-0.0219136 ]]

import matplotlib.animation as animation
import time

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot()

def animate(i):
    
    x = np.linspace(0, len(dataArray), len(dataArray))
    y = []

    for eachLine in x:
        y.append(eachLine * 10)
            
    ax1.clear()
    ax1.plot(x, y, color = 'red', alpha = 0.5)

ani = animation.FuncAnimation(fig, func = animate)

plt.show()

动画的基础是一种机制,其中动画函数设置要为初始图形设置更改的值。在本例中,线宽为 3,颜色为红色,x 和 y 为空列表。 xy数据在动画函数中通过i链接到帧数。

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

fig = plt.figure() 

# marking the x-axis and y-axis 
axis = plt.axes(xlim =(0, 500), ylim =(0, 1000)) 

x = np.linspace(0, 500, 500) 
y = x*2
# initializing a line variable 
line, = axis.plot([], [], lw=2, color='r') 

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

anim = FuncAnimation(fig, animate, frames=500, blit=True, repeat=False) 
plt.show()