欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Matplotlib的animation.FuncAnimation画动画

程序员文章站 2022-07-13 21:58:44
...
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x = np.linspace(0, 10, 100)
y = 1+ np.random.randint(0,10,size=(100,1))*0.1
print(y)

fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')
line2, = ax.plot(x, y-0.1, color='k')


print(type(line))

def update(num, x, y, line):
    line.set_data(x[:num], y[:num])
    line.axes.axis([0, 10, 0, 2])
    line2.set_data(x[:num], (y-0.1)[:num])
    line2.axes.axis([0, 10, 0, 2])
    return line,line2  # 可以同时画两个或多个线,只要在update函数中返回多个线即可


ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line],
                              interval=25, blit=True)
ani.save('test.gif')
plt.show()

用Matplotlib的animation.FuncAnimation画动画