Python 具有时间相关参数的Matplotlib动画

Python 具有时间相关参数的Matplotlib动画,python,matplotlib,animation,Python,Matplotlib,Animation,我是新使用python的,我想问你一个关于我当前代码的问题。我正在解一个偏微分方程(空间中的1D),我想每次为给定的数值解制作一个动画,但我不想每次都保存解的所有数组(因为这样做效率不高) 为简单起见,我只向你们展示微分方程的解析解 我每次尝试制作动画时都会尝试制作一个情节,但正如我在其他地方读到的,由于plt.pause()的原因,它并没有太大的效率 将numpy导入为np 输入数学 从matplotlib导入pyplot作为plt pi=math.pi xmin=0.0 xmax=10.0

我是新使用python的,我想问你一个关于我当前代码的问题。我正在解一个偏微分方程(空间中的1D),我想每次为给定的数值解制作一个动画,但我不想每次都保存解的所有数组(因为这样做效率不高)

为简单起见,我只向你们展示微分方程的解析解

我每次尝试制作动画时都会尝试制作一个情节,但正如我在其他地方读到的,由于plt.pause()的原因,它并没有太大的效率

将numpy导入为np
输入数学
从matplotlib导入pyplot作为plt
pi=math.pi
xmin=0.0
xmax=10.0
N=100#点数
x=np.arange(xmin,xmax,(xmax-xmin)/N)
def溶液(t):
p=np.exp(-x**2/(4*k*t))/(np.sqrt(4*pi*k*t))
返回p
t_final=10.0
t_初始值=0.0
t=t_首字母
dt=0.1
k=1.0

而t以下是如何使用
FuncAnimation
生成所需动画

import numpy as np
import math
from matplotlib import pyplot as plt

pi = math.pi
xmin = 0.0
xmax = 10.0
N = 100 #number of points
x = np.arange(xmin, xmax , (xmax-xmin)/N)

t_initial = 0.0
t_final = 10.0
dt = 0.1
k = 1.0

fig, ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('P')
plotLine, = ax.plot(x, np.zeros(len(x))*np.NaN, 'r-')
plotTitle = ax.set_title("t=0")
ax.set_ylim(0,1.)
ax.set_xlim(xmin,xmax)


def solution(t):
    p = np.exp(-x**2/(4*k*t))/(np.sqrt(4.*pi*k*t))
    return p


def animate(t):
    pp = solution(t)
    plotLine.set_ydata(pp)
    plotTitle.set_text(f"t = {t:.1f}")
    #ax.relim() # use if autoscale desired
    #ax.autoscale()
    return [plotLine,plotTitle]



ani = animation.FuncAnimation(fig, func=animate, frames=np.arange(t_initial, t_final+dt, dt), blit=True)
plt.show()

以下是如何使用
FuncAnimation
生成所需的动画

import numpy as np
import math
from matplotlib import pyplot as plt

pi = math.pi
xmin = 0.0
xmax = 10.0
N = 100 #number of points
x = np.arange(xmin, xmax , (xmax-xmin)/N)

t_initial = 0.0
t_final = 10.0
dt = 0.1
k = 1.0

fig, ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('P')
plotLine, = ax.plot(x, np.zeros(len(x))*np.NaN, 'r-')
plotTitle = ax.set_title("t=0")
ax.set_ylim(0,1.)
ax.set_xlim(xmin,xmax)


def solution(t):
    p = np.exp(-x**2/(4*k*t))/(np.sqrt(4.*pi*k*t))
    return p


def animate(t):
    pp = solution(t)
    plotLine.set_ydata(pp)
    plotTitle.set_text(f"t = {t:.1f}")
    #ax.relim() # use if autoscale desired
    #ax.autoscale()
    return [plotLine,plotTitle]



ani = animation.FuncAnimation(fig, func=animate, frames=np.arange(t_initial, t_final+dt, dt), blit=True)
plt.show()

这种方式使用我编写的库。有了它,我只需要更改代码中的几行代码:主要是使用赛璐珞的少数调用,并稍微更改图例创建

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')
将numpy导入为np
输入数学
从matplotlib导入pyplot作为plt
从赛璐珞进口照相机
pi=math.pi
xmin=0.0
xmax=10.0
N=100#点数
x=np.arange(xmin,xmax,(xmax-xmin)/N)
def溶液(t):
p=np.exp(-x**2/(4*k*t))/(np.sqrt(4*pi*k*t))
返回p
t_final=10.0
t_初始值=0.0
t=t_首字母
dt=0.1
k=1.0
图=plt.图()
摄像机=摄像机(图)
plt.xlabel('x')
plt.ylabel('P')

而t这种方式使用了我编写的库。有了它,我只需要更改代码中的几行代码:主要是使用赛璐珞的少数调用,并稍微更改图例创建

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')
将numpy导入为np
输入数学
从matplotlib导入pyplot作为plt
从赛璐珞进口照相机
pi=math.pi
xmin=0.0
xmax=10.0
N=100#点数
x=np.arange(xmin,xmax,(xmax-xmin)/N)
def溶液(t):
p=np.exp(-x**2/(4*k*t))/(np.sqrt(4*pi*k*t))
返回p
t_final=10.0
t_初始值=0.0
t=t_首字母
dt=0.1
k=1.0
图=plt.图()
摄像机=摄像机(图)
plt.xlabel('x')
plt.ylabel('P')

虽然tI会建议使用。我知道,似乎是我在寻找的东西,但如何在我的代码中实现?我会建议使用。我知道,似乎是我在寻找的东西,但如何在我的代码中实现?