Python 3.x 在Matplotlib.animation中使用blit=true后,如何保存精确的动画?
我想以傅里叶级数的方式添加圆。但在动画中,前一帧也存在。然后我添加Python 3.x 在Matplotlib.animation中使用blit=true后,如何保存精确的动画?,python-3.x,blit,matplotlib-animation,Python 3.x,Blit,Matplotlib Animation,我想以傅里叶级数的方式添加圆。但在动画中,前一帧也存在。然后我添加blit=True,它解决了我的问题。但当我尝试将动画保存到mp4文件时,我得到了所有叠加的帧。我是新来的matoplotlib。如果有人能告诉我如何解决这个问题,那会有很大帮助。我还不知道如何使用init_func以下是我的代码: import matplotlib.pyplot as plt import numpy as np from matplotlib import animation plt.rcParams[
blit=True
,它解决了我的问题。但当我尝试将动画保存到mp4文件时,我得到了所有叠加的帧。我是新来的matoplotlib
。如果有人能告诉我如何解决这个问题,那会有很大帮助。我还不知道如何使用init_func
以下是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = 'C:/ffmpeg/bin/ffmpeg'
fig = plt.figure()
plt.axis([-50, 50, -50, 50])
ax = plt.gca()
ax.set_aspect(1)
def init():
# line.set_data([],[])
print('hello1')
return []
def animate(t):
print(t)
x = 0
y = 0
patches = []
balls = []
for i in range(3):
prevx = x
prevy = y
n = 2*i + 1
radius = 15 * (4/(n*np.pi))
x += radius * np.cos(n * t)
y += radius * np.sin(n * t)
#updating main circle
patches.append(ax.add_patch(plt.Circle((prevx,prevy),radius,color='g',fill = False)))
#updating small ball
patches.append(ax.add_patch(plt.Circle((x,y),0.7, color = 'b')))
line, = ax.plot([prevx, x], [ prevy, y], color = 'k', lw=1)
patches.append(line)
return patches
anim = animation.FuncAnimation(fig, animate, frames = np.arange(0,20,0.2),
init_func=init, interval=200, blit=True)
FFwriter = animation.FFMpegWriter(fps = 10, bitrate=5000)
anim.save('circle_add.mp4',writer = FFwriter)
plt.show()