我应该如何为python matplotlib中的粒子设置动画? [关闭] - python

Closed. This question needs debugging details。它当前不接受答案。

想改善这个问题吗?更新问题,以使为on-topic。

5年前关闭。

Improve this question

在我的作业中,我被要求编写一个脚本来模拟容器中的气体颗粒。

现在我已经完成了数学部分,到目前为止,它的工作方式如下:

1)输入一个包含位置坐标和运动矢量的初始列表
2)然后创建一个转换后的列表,其中包含所有x坐标和y坐标,每个都在一个单独的子列表中,以供以后绘制
3)然后运行我编写的一系列函数,该函数会在间隔之后更新列表中的位置和向量
4)再次转换列表
5)依此类推

但是我根本不知道如何为它们设置动画?

我想我需要这样的东西:

1)画一个圆用作容器+初始粒子/位置
2)保留圈子并更新列表
3)画圈和更新列表
4)等等,速度非常快

python大神给出的解决方案

这里有个简单的例子:

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

fig, ax = plt.subplots()
points, = ax.plot(np.random.rand(10), 'o')
ax.set_ylim(0, 1)

def update(data):
    points.set_ydata(data)
    return points,

def generate_points():
    while True:
        yield np.random.rand(10)  # change this

ani = animation.FuncAnimation(fig, update, generate_points, interval=300)
ani.save('animation.gif', writer='imagemagick', fps=4);
plt.show()