【问题标题】:Can't get my program to animate multiple patches in python matplotlib无法让我的程序在 python matplotlib 中为多个补丁设置动画
【发布时间】:2016-11-19 02:01:31
【问题描述】:

我正在尝试在 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()

【问题讨论】:

    标签: python animation matplotlib plot simulation


    【解决方案1】:
    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()
    

    【讨论】:

    • 非常感谢!我是matplotlib的新手,所以我花了一段时间才弄清楚哈哈如果你不介意,你能看看我发布的一个更新的问题吗? stackoverflow.com/questions/38446295/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多