使用matplotlib与子图和ArtistAnimation的动画

abr*_*add 9 python animation matplotlib

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

在此输入图像描述

我一直收到错误:AttributeError:'list'对象没有属性'set_visible'.我用Google搜索了(就像你一样)并偶然发现了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()
Run Code Online (Sandbox Code Playgroud)

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

阿德里安

tac*_*ell 20

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

更改

 im2 = ax2.plot(image[0:time,5,5])
Run Code Online (Sandbox Code Playgroud)

 im2, = ax2.plot(image[0:time,5,5])
Run Code Online (Sandbox Code Playgroud)

添加逗号取消打包一个列表的长度 im2

至于你的第二个问题,我们试着每个线程只有一个问题,所以请打开一个新问题.