使用matplotlib与子图和ArtistAnimation的动画

时间:2013-07-25 09:09:57

标签: python animation matplotlib

我正在进行图像分析,我想创建最终结果的动画,其中包括2D数据的时间序列和单个像素的时间序列图,使得1D图更新为2D动画进展。然后将它们并排设置在一个子图中。下面的链接有一个最终结果的图像,理想情况下是动画。

enter image description here

我一直收到错误:AttributeError:'list'对象没有属性'set_visible'。我用谷歌搜索它(就像你一样)并偶然发现http://matplotlib.1069221.n5.nabble.com/Matplotlib-1-1-0-animation-vs-contour-plots-td18703.html,其中一个人打了代码来设置set_visible属性。不幸的是,plot命令似乎没有这样的属性,所以我不知道如何制作动画。我已经将猴子补丁包含在下面的最小工作示例中(注释掉了)以及第二个“im2”,它也被注释掉了,它应该适用于任何试图运行代码的人。显然它会给你两个2D绘图动画。最小的工作示例如下:

#!/usr/bin/env python

import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np
import types

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

#setup figure
fig = plt.figure()
ax1=fig.add_subplot(1,2,1)
ax2=fig.add_subplot(1,2,2)

#set up list of images for animation
ims=[]
for time in xrange(np.shape(image)[1]):
    im = ax1.imshow(image[time,:,:])
 #   im2 = ax2.imshow(image2[time,:,:])
    im2 = ax2.plot(image[0:time,5,5])
 #   def setvisible(self,vis): 
 #       for c in self.collections: c.set_visible(vis) 
 #    im2.set_visible = types.MethodType(setvisible,im2,None) 
 #    im2.axes = plt.gca() 

    ims.append([im, im2])

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

我也很好奇是否有人知道一种很酷的方法来突出显示1D数据的像素,或者甚至从像素到最右边的子图画一条线,以便它们在某些地方“连接”方式。

阿德里安

1 个答案:

答案 0 :(得分:17)

plot返回艺术家列表(因此错误指的是列表)。这样您就可以像plot一样致电lines = plot(x1, y1, x2, y2,...)

更改

 im2 = ax2.plot(image[0:time,5,5])

 im2, = ax2.plot(image[0:time,5,5])

添加逗号将一个列表的长度打包到im2

关于你的第二个问题,我们尝试在每个帖子上只有一个问题,所以请打开一个新问题。