使用带有子图和 ArtistAnimation 的 matplotlib 制作动画 [英] Animation using matplotlib with subplots and ArtistAnimation

查看:64
本文介绍了使用带有子图和 ArtistAnimation 的 matplotlib 制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

阿德里安

推荐答案

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

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,...).

改变

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

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

添加逗号将长度为一的列表解包到 im2

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

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

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

这篇关于使用带有子图和 ArtistAnimation 的 matplotlib 制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆