Python:使用 Python 对同一图形的多次迭代进行绘图和动画处理

Python: Graphing and animating multiple iterations of the same graph with Python

你好,我正在尝试创建一个由 15 个向左移动的高斯图组成的电影(这基本上就是代码应该做的) 但是,我关于如何创建 for 循环以创建 15 个图形的想法并没有创建超过 1 个,它只是加快了动画速度。 类似的代码适用于 matlab。它创建了 15 条不同的高斯曲线。 这是我的代码示例。 任何帮助,将不胜感激。 谢谢

import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.animation as animation

Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m
#e=2.78

#E0=0+(1.0567e-34)*x*i/m

fig, ax = plt.subplots()

pass
x = np.arange(0, 3.4e-3, 1.7e-5)        # x-array, third number is interval here, x is energy
line, = ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))


def animate(i):

    for p in xrange(1,3):   
        line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2))  # update the data
        return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init,
    interval=10, blit=True)
Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)

ani.save('QHanimati.mp4', writer=writer)

plt.show()

您目前的代码中只有一行。此行得到更新。如果你想有更多的线路,你需要创建更多的线路。
然后您还需要更新所有这些行。

(由于示例中 p 的作用不明确,我在这里将其作为一些递增的数字。我还将其限制为 8 条曲线,以免图像过于拥挤。)

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

Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m

fig, ax = plt.subplots()

n = 8 # number of lines
x = np.arange(0, 3.4e-3, 1.7e-5)        
lines = [ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))[0] for _ in range(n)]


def animate(i):
    for ln, line in enumerate(lines):
        p = (ln+1)/10.
        line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2))  # update the data
    return lines

#Init only required for blitting to give a clean slate.
def init():
    for line in lines:
        line.set_ydata(np.ma.array(x, mask=True))
    return lines

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init,
    interval=10, blit=True)

plt.show()