无法让我的程序在 python matplotlib 中为多个补丁设置动画

Can't get my program to animate multiple patches in python matplotlib

我正在尝试在 matplotlib (python) 中为两个不同的粒子设置动画。我刚刚想出了一种在 matplotlib 中为一个粒子设置动画的方法,但是我在尝试让程序处理多个粒子时遇到了困难。有谁知道出了什么问题以及如何解决?

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

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(5, 4.5)

ax = plt.axes(xlim=(0, 100), ylim=(0, 100))
enemy = plt.Circle((10, -10), 0.75, fc='r')
agent = plt.Circle((10, -10), 0.75, fc='b')



def init():
    #enemy.center = (5, 5)
    #agent.center = (5, 5)
    ax.add_patch(agent)
    ax.add_patch(enemy)

    return []

def animationManage(i,agent,enemy):
    patches = []

    enemy.center = (5, 5)
    agent.center = (5, 5)

    enemy_patches = animateCos(i,agent)
    agent_patches = animateLine(i,enemy)

    patches[enemy_patches, agent_patches]

    #patches.append(ax.add_patch(enemy_patches))
    #patches.append(ax.add_patch(agent_patches))

    return enemy_patches

def animateCirc(i, patch):
    # It seems that i represents time step
    x, y = patch.center
    # 1st constant = position and 2nd constant = trajectory
    x = 50 + 30 * np.sin(np.radians(i))
    y = 50 + 30 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

def animateLine(i, patch):
    x, y = patch.center
    x = x + 1
    y = x+ 1
    patch.center = (x, y)
    return patch,


def animateCos(i, patch):
    x, y = patch.center
    x = x + 0.2
    y = 50 + 30 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

def animateSin(i, patch):
    x, y = patch.center
    x = x + 0.2
    y = 50 + 30 * np.sin(np.radians(i))
    patch.center = (x, y)
    return patch,


anim = animation.FuncAnimation(fig, animationManage, 
                               init_func=init, 
                               frames=360,
                               fargs=(agent,enemy,),
                               interval=20,
                               blit=True)


plt.show()

为一个粒子设置动画的工作代码

import numpy as np

from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(5, 4.5)

ax = plt.axes(xlim=(0, 100), ylim=(0, 100))
enemy = plt.Circle((10, -10), 0.75, fc='r')
agent = plt.Circle((10, -10), 0.75, fc='b')

def init():
    enemy.center = (5, 5)
    agent.center = (5, 5)
    ax.add_patch(enemy)
    ax.add_patch(agent)
    return enemy,

def animateCirc(i, patch):
    # It seems that i represents time step
    x, y = patch.center
    # 1st constant = position and 2nd constant = trajectory
    x = 50 + 30 * np.sin(np.radians(i))
    y = 50 + 30 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

def animateLine(i, patch):
    x, y = patch.center
    x = x + 1
    y = x+ 1
    patch.center = (x, y)
    return patch,


def animateCos(i, patch):
    x, y = patch.center
    x = x + 0.2
    y = 50 + 30 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

def animateSin(i, patch):
    x, y = patch.center
    x = x + 0.2
    y = 50 + 30 * np.sin(np.radians(i))
    patch.center = (x, y)
    return patch,


anim = animation.FuncAnimation(fig, animateCos, 
                               init_func=init, 
                               frames=360,
                               fargs=(enemy,),
                               interval=20,
                               blit=True)

plt.show()
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(5, 4.5)

ax = plt.axes(xlim=(0, 100), ylim=(0, 100))
enemy = plt.Circle((10, -10), 0.75, fc='r')
agent = plt.Circle((10, -10), 0.75, fc='b')


def init():
    enemy.center = (5, 5)
    agent.center = (5, 5)
    ax.add_patch(agent)
    ax.add_patch(enemy)

    return []


def animationManage(i,agent,enemy):
    animateCos(i,enemy)
    animateLine(i,agent)
    return []


def animateLine(i, patch):
    x, y = patch.center
    x += 0.25
    y += 0.25
    patch.center = (x, y)
    return patch,


def animateCos(i, patch):
    x, y = patch.center
    x += 0.2
    y = 50 + 30 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

anim = animation.FuncAnimation(fig, animationManage,
                               init_func=init,
                               frames=360,
                               fargs=(agent,enemy,),
                               interval=20,
                               blit=True,
                               repeat=True)


plt.show()