2013-07-27 153 views
5

我正在尝试使用matplotlib.ArtistAnimation来动画两个子图。我希望X轴随着动画的进展而增加值,这样动画的总长度是100,但是在任何时候,子图都只给我提供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://stackoverflow.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() 

会工作。

如果您需要跑得更快,您需要使用用于blitting的边框进行游戏,以便更新坐标轴标签。

+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

如果您正在使用blitting,则可以在每次更改y/x轴时调用pyplot.draw()来重新绘制整个图形。

这会更新整个图形,所以速度相对较慢,但如果不将其称为多个项目,则可以接受。