2016-07-15 6 views
2

Я пытаюсь оживить две разные частицы в matplotlib (python). Я просто вычислил способ анимации одной частицы в 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() 

ответ

2
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() 
+0

Спасибо так много! Я новичок в matplotlib, поэтому мне потребовалось некоторое время, чтобы выяснить ха-ха. Если вы не возражаете, можете ли вы взглянуть на более свежий вопрос, который я разместил? http://stackoverflow.com/questions/38446295/cannot-animate-clones-of-the-same-patch-in-python-matplotlib – user3377126