Python多体动画不起作用

标签 python animation matplotlib

我被一个Python动画困住了,在这个动画中,我试图对最初排列在二维六角形晶格中并逐渐按照规则展开的粒子系统进行动画处理:xpos1[i]=xpos1[i]+ L/10.0。如果任何粒子超出窗口限制,它们就会从另一侧带入

if xpos1[i]>L*3:                    # translate back the particle if it goes out of window limit 0 to L*3
    xpos1[i]=xpos1[i]-L*3
elif xpos1[i]<0:
    xpos1[i]=L*3-xpos1[i]

并且所有位置的更新都存储在两个列表xpos1和ypos1中。这需要几个时间步骤来完成。

我希望通过将系统转换为动画来可视化系统的时间演变。我的代码如下。我以前从未做过 matplotlib 动画,实际上是从另一个运行良好的程序复制了“动画”部分。但它对我不起作用。

from numpy import*
import matplotlib.pyplot as plt
import matplotlib.animation as animation

sigma=3.4e-10           # dist of closest approach
L=4.8e-10           # lattice constant = sigma*2**0.5 (Let)

xpos1=zeros(18,float)
ypos1=zeros(18,float)

# ~~~~~~~~~~~  Setting up the hexagonal lattice ~~~~~~~~~~~~~~~~~~~~~~
k=0
for x in range(0,6,1):
    for y in range(0,6,1):
        if (x+y)%2==0:
            xpos1[k]=x*L*.5+.25*L
            ypos1[k]=y*L*.5+.25*L
            k=k+1

#~~~~~~~~~~~~~~~~~~TIME EVOLUTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t = 4.5e-12
iteration=1
while t<=1e-9:
    for i in range(18):
        xpos1[i]=xpos1[i]+L/10.0
        ypos1[i]=ypos1[i]+L/10.0
        if xpos1[i]>L*3:                    # translate back the particle if it goes out of window limit 0 to L*cell
            xpos1[i]=xpos1[i]-L*3
        elif xpos1[i]<0:
            xpos1[i]=L*3-xpos1[i]
        if ypos1[i]>L*3:                    # translate back the particle if it goes out of window limit 0 to L*cell
            ypos1[i]=ypos1[i]-L*3
        elif ypos1[i]<0:
            ypos1[i]=L*3-ypos1[i]
    t = t + 4.5e-12

#~~~~~~~~~~~~~~~~~ ANIMATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def simData():
    for i in range(18):
        x=xpos1[i]
        y=ypos1[i]
        yield x,y


def simPoints(simData):
    x,y= simData[0],simData[1]
    line.set_data(x,y)
    return line
fig = plt.figure()
ax = fig.add_subplot(111)

line,= ax.plot([],[],'bo',ms=8)
ax.set_ylim(0 , L*3)
ax.set_xlim(0 , L*3)

ani = animation.FuncAnimation(fig, simPoints, simData, blit=True ,   interval=200)

plt.show()

有人可以告诉我如何成功制作动画吗?

最佳答案

您的动画update(以及init,如果有的话)必须返回一个iterable

def simPoints(simData):
    x, y = simData[0], simData[1]
    line.set_data(x, y)
    return line,            # added a comma to return a tuple

如果您使用的是 Mac 操作系统,您可能还需要设置 blit=False

ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=200)

编辑:

这是一个显示 18 个随机点的最小工作示例 - 您必须将随机生成更改为您想要的晶格上点的模式。

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

x = np.random.random(18)
y = np.random.random(18)

def simData():
    """updates the points position on your lattice.
    replace with your own code - can call a helper function to accomplish this task 
    """
    x = np.random.random(18)
    y = np.random.random(18)
    yield x, y

def simPoints(simData):
    """initializes the points position on your lattice.
    replace with your own code - can call a helper function to accomplish this task 
    """
    x = np.random.random(18)
    y = np.random.random(18)
    line.set_data(x, y)
    return line,

fig = plt.figure()
ax = fig.add_subplot(111)

line, = ax.plot(x, y,'bo', ms=8)

ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=200)

plt.show()

关于Python多体动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36759957/

相关文章:

python - 使用 jsonpickle (python) 进行类型演化

ios - UIView - 带有持续时间的动画似乎忽略了持续时间

python - 如何使用 matplotlib 从数据帧创建简单的动画图

python - 如何确保matplotlib图x轴上标签之间的间距均匀?

javascript - HTML5 Canvas,无法同时运行多个动画

python - 如何找到两个图像之间的相关性

python - 在 Mayavi 中以 0 为中心的颜色图

python - 在 pydev 中安装 Numpy(eclipse)

python - 为什么 3d 箭袋图的箭头指向错误的方向?

android - 钛制安卓设备中的动画 SCSS