13

I am working on an image analysis and I want to create an animation of the final results that includes the time-sequence of 2D data and a plot of the time sequences at a single pixel such that the 1D plot updates as the 2D animation progresses. Then set them up in a subplot side by side The link below has an image of the end result which would ideally be animated.

enter image description here

I keep getting an error: AttributeError: 'list' object has no attribute 'set_visible'. I googled it (as you do) and stumbled across http://matplotlib.1069221.n5.nabble.com/Matplotlib-1-1-0-animation-vs-contour-plots-td18703.html where one guy duck punches the code to set the set_visible attribute. Unfortunately, the plot command does not seem to have such an attribute so I am at a loss as to how I can produce the animation. I have included the monkey patch in the minimal working example below (commented out) as well as a second 'im2' that is also commented out which should work for anyone trying to run the code. Obviously it will give you two 2D plot animations. Minimal working example is as follows:

#!/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()

I was also curious as to whether anyone knew of a cool way to highlight the pixel that the 1D data is being extracted from, or even draw a line from the pixel to the rightmost subplot so that they are 'connected' in some way.

Adrian

4

1 回答 1

23

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

改变

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

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

添加逗号将长度一个列表解压缩到im2

至于您的第二个问题,我们尝试在 SO 上的每个线程只有一个问题,所以请打开一个新问题。

于 2013-07-25T16:26:53.937 回答