2013-07-27 4 views
5

matplotlib.ArtistAnimation을 사용하여 두 개의 부 그림을 애니메이션화하려고합니다. 애니메이션의 총 길이가 100이되도록 x 축의 값이 증가하기를 원하지만 언제든지 서브 플로트는 0-24의 시간 값을 제시하고 나면 최대 100까지 반복합니다.matplotlib 애니메이션을 사용하여 x 축 값 업데이트

큰 예제는 here입니다. 링크는 FuncAnimation을 사용하고 plot().axes.set_xlim()을 사용하고 x 값을 증가시켜 롤링 방식으로 x 축 레이블을 업데이트합니다. 코드는 제공된 링크의 YouTube 동영상 아래 링크를 통해 제공됩니다.

이러한 결과를 복제하려는 시도를 보여 주지만 x- 제한은 시간이 증가하는 대신 최종 값을 대신하는 것처럼 보이는 아래 코드를 추가했습니다. 나는 또한 서브 플롯에서 볼 수있는 창에서 값을 플로팅하여 x 축 값을 증가시키지 않고 (축과 대조적으로) 솔루션을 증가 시키려고 시도했습니다. 또한 자동 크기 조정을 구현하려고 시도했지만 x 축은 여전히 ​​업데이트되지 않습니다.

나는 또한 실질적으로 동일한 문제인 this question을 발견했지만 그 질문에는 결코 대답하지 않았습니다.

import matplotlib.pylab as plt 
import matplotlib.animation as anim 
import numpy as np 

#create image with format (time,x,y) 
image = np.random.rand(100,10,10) 

#setup figure 
fig = plt.figure() 
ax1=fig.add_subplot(1,2,1) 
ax2=fig.add_subplot(1,2,2) 
#set up viewing window (in this case the 25 most recent values) 
repeat_length = (np.shape(image)[0]+1)/4 
ax2.set_xlim([0,repeat_length]) 
#ax2.autoscale_view() 
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])]) 

#set up list of images for animation 

ims=[] 
for time in xrange(np.shape(image)[0]): 

    im = ax1.imshow(image[time,:,:]) 
    im2, = ax2.plot(image[0:time,5,5],color=(0,0,1)) 
    if time>repeat_length: 
     lim = ax2.set_xlim(time-repeat_length,time) 

    ims.append([im, im2]) 


#run animation 
ani = anim.ArtistAnimation(fig,ims, interval=50,blit=False) 
plt.show() 

가 난 단지 두 번째 부가 적 줄거리 (ax2)는 x 축 값을 업데이트 할 :

여기 내 코드입니다.

도움을 주시면 감사하겠습니다.

+1

http://itackoverflow.com/questions/17558096/animated-title-in-matplotlib/17562747#17562747 <- blit에 라벨을 업데이트하려면 – tacaswell

+0

http://stackoverflow.com/questions/17888593/display-sequence -of-images-using-matplotlib – tacaswell

+0

blit을 사용할 때 애니메이션이 실행되지 않는다. 이유는 확실하지 않다. – abradd

답변

5

당신이

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

#create image with format (time,x,y) 
image = np.random.rand(100,10,10) 

#setup figure 
fig = plt.figure() 
ax1 = fig.add_subplot(1,2,1) 
ax2 = fig.add_subplot(1,2,2) 
#set up viewing window (in this case the 25 most recent values) 
repeat_length = (np.shape(image)[0]+1)/4 
ax2.set_xlim([0,repeat_length]) 
#ax2.autoscale_view() 
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])]) 

#set up list of images for animation 


im = ax1.imshow(image[0,:,:]) 
im2, = ax2.plot([], [], color=(0,0,1)) 

def func(n): 
    im.set_data(image[n,:,:]) 

    im2.set_xdata(np.arange(n)) 
    im2.set_ydata(image[0:n, 5, 5]) 
    if n>repeat_length: 
     lim = ax2.set_xlim(n-repeat_length, n) 
    else: 
     # makes it look ok when the animation loops 
     lim = ax2.set_xlim(0, repeat_length) 
    return im, im2 

ani = animation.FuncAnimation(fig, func, frames=image.shape[0], interval=30, blit=False) 

plt.show() 

이 작동 블리 팅하지 필요성을 합니다.

더 빨리 달리려면 블리 팅에 사용 된 경계 상자로 게임을해야 축 레이블이 업데이트됩니다.

+0

ArtistAnimation을 사용하여 실행시킬 수 있었지만 결국에는 총알을 비트하고 코드를 다시 작성하기를 바랬습니다. 그것은 매끄럽게 달리고있는 것처럼 보인다. – abradd

0

축이 움직이지만 속도는 매우 느립니다.

import matplotlib.pylab as plt 
import matplotlib.animation as anim 
import numpy as np 


image = np.random.rand(100,10,10) 
repeat_length = (np.shape(image)[0]+1)/4 

fig = plt.figure() 
ax1 = ax1=fig.add_subplot(1,2,1) 
im = ax1.imshow(image[0,:,:]) 

ax2 = plt.subplot(122) 
ax2.set_xlim([0,repeat_length]) 
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])]) 
im2, = ax2.plot(image[0:0,5,5],color=(0,0,1)) 

canvas = ax2.figure.canvas 

def init(): 
    im = ax1.imshow(image[0,:,:]) 
    im2.set_data([], []) 
    return im,im2, 

def animate(time): 
    time = time%len(image) 
    im = ax1.imshow(image[time,:,:]) 
    im2, = ax2.plot(image[0:time,5,5],color=(0,0,1)) 
    if time>repeat_length: 
     print time 
     im2.axes.set_xlim(time-repeat_length,time) 
     plt.draw() 
    return im,im2, 

ax2.get_yaxis().set_animated(True) 

# call the animator. blit=True means only re-draw the parts that have changed. 
animate = anim.FuncAnimation(fig, animate, init_func=init, 
           interval=0, blit=True, repeat=True) 

plt.show() 
+0

매번'imshow' 오브젝트를 다시 만들 필요는 없으며 대신'set_data'를 사용하십시오. – tacaswell

0

블리 팅을 사용하는 경우 y/x 축을 변경할 때마다 pyplot.draw()을 호출하여 전체 그림을 다시 그릴 수 있습니다.

이것은 전체 그림을 업데이트하므로 상대적으로 느리지 만 많은 항목이라고 부르지 않아도됩니다.