2013-11-14 5 views
4

시간이 지남에 따라 색상이 변경되도록 원형 배열에 애니메이션을 적용하려고합니다.python/matplotlib의 패치 객체에 애니메이션 적용

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

nx = 20 
ny = 20 

fig = plt.figure() 
plt.axis([0,nx,0,ny]) 
ax = plt.gca() 
ax.set_aspect(1) 

for x in range(0,nx): 
    for y in range(0,ny): 
     ax.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='r')) 

plt.show() 

이 어떻게 (기능 초기화 정의) 내가 예를 이용한 애니메이션을 생성 할 수 있도록) (애니메이션 않는다 :

animation.FuncAnimation(fig, animate, initfunc=init,interval=200, blit=True) 
+0

참조 코멘트 [이 질문] (http://stackoverflow.com/questions/19955073/simple-matplotlib-animation) :이 같은 애니메이션 원의 색상으로 변경할 수 있습니다 예 : –

답변

6

를 하나의 프레임의 예는 다음 코드에 의해 생성 된 일부 링크에

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

nx = 20 
ny = 20 

fig = plt.figure() 
plt.axis([0,nx,0,ny]) 
ax = plt.gca() 
ax.set_aspect(1) 

def init(): 
    # initialize an empty list of cirlces 
    return [] 

def animate(i): 
    # draw circles, select to color for the circles based on the input argument i. 
    someColors = ['r', 'b', 'g', 'm', 'y'] 
    patches = [] 
    for x in range(0,nx): 
     for y in range(0,ny): 
      patches.append(ax.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color=someColors[i % 5]))) 
    return patches 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=10, interval=20, blit=True) 
plt.show() 
+0

그레이트 : 몰리, 정말 고마워! – BeMuSeD

+0

@ user2992602,이 문제를 해결하는 데 도움이 되었다면 [이 대답 수락] (http://meta.stackexchange.com/a/5235)을 고려하십시오. – falsetru

관련 문제