python matlabplot animation_使用matplotlib大选的和ArtistAnimation动画

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.

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

解决方案

plot returns a list of artists (hence why the error is referring to a list). This is so you can call plot like lines = plot(x1, y1, x2, y2,...).

Change

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

to

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

Adding the comma un-packs the length one list into im2

As for you second question, we try to only have one question per thread on SO so please open a new question.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值