2 回答

TA贡献1770条经验 获得超3个赞
这种方式使用了我写的一个库,celluloid。有了它,我只需要从您的代码中更改几行:主要是使用赛璐珞的少数调用并稍微更改图例创建。
import numpy as np
import math
from matplotlib import pyplot as plt
from celluloid import Camera
pi = math.pi
xmin = 0.0
xmax = 10.0
N = 100 #number of points
x = np.arange(xmin, xmax , (xmax-xmin)/N)
def solution(t):
p = np.exp(-x**2/(4*k*t))/(np.sqrt(4.*pi*k*t))
return p
t_final = 10.0
t_initial = 0.0
t = t_initial
dt = 0.1
k = 1.0
fig = plt.figure()
camera = Camera(fig)
plt.xlabel('x')
plt.ylabel('P')
while t<t_final:
t +=dt
pp = solution(t)
line = plt.plot(x, pp, 'r-')
plt.legend(line, ['t={:.1f}'.format(t)], loc='upper right')
camera.snap()
animation = camera.animate()
animation.save('animation.mp4')
添加回答
举报