使用matplotlib.animation

时间:2019-02-06 13:25:46

标签: python matplotlib animation

我正在尝试使用matplotlib制作动画gif。我在这里找到了简单的示例:https://tomroelandts.com/articles/how-to-create-animated-gifs-with-python 现在,我正在尝试使其适应我的问题。下面是我计算出的代码:

from __future__ import division
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from mpl_toolkits.mplot3d import proj3d 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

class AnimatedGif:
    def __init__(self,radius = 10):
        ############################################################
        self.fig = plt.figure(frameon=False,figsize=(12,10))
        self.fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
        ax = Axes3D(self.fig)
        ax.set_xlim((-.7*radius,.7*radius))
        ax.set_ylim((-.7*radius,.7*radius))
        ax.set_zlim((0*radius,1.4*radius))
        ax.axis('off')
        ax.xaxis.pane.set_edgecolor('black')
        ax.yaxis.pane.set_edgecolor('black')
        ax.xaxis.pane.fill = False
        ax.yaxis.pane.fill = False
        ax.zaxis.pane.fill = False
        self.images = []
    ####################################################################
    def add(self,step, area='',azim = 0, elev = 7,color = 'k',numOfLayer = 10,Length = 1.,alpha = 0.8,ids = False):
        from mpl_toolkits.mplot3d import Axes3D
        import matplotlib as mpl
        from mpl_toolkits.mplot3d.art3d import Poly3DCollection
        from mpl_toolkits.mplot3d import proj3d
        ################################################
        ax = self.fig.gca()
        verts = [zip([step*0.5+0.,5,1.,2,1,2],[0.,3,2.,2,1,2],[0.,9,3.,2,1,2])]
        pc = Poly3DCollection(verts,alpha = alpha,linewidths=1, facecolor = color)
        pc.set_edgecolor('k')
        ax.add_collection3d(pc)
        ax.view_init(azim = azim,elev=elev)
        plt_im = ax
        self.images.append([plt_im])
    ####################################################################
    def save(self, filename):
        animation = anim.ArtistAnimation(self.fig, self.images)
        print animation
        animation.save(filename, writer='imagemagick', fps=1)
##################################################################
endStep = 100
stepsize = 5
##################################################################
animated_gif = AnimatedGif()
step = 0 
area = 0 
animated_gif.add(step,area=str(area))
images = []
for step in range(stepsize+1, endStep,stepsize):
    area = 10
    animated_gif.add(step, area=str(area))

animated_gif.save('cell-animated1.gif')

我需要使用Poly3DCollection在3d上制作一堆多边形,它们随时间步移。在这里,我只是简单地说明了多边形沿x轴逐步移动的情况。但是没有输出。输出仅为空白白色。


我的困惑主要是由于缺乏使用ArtistAnimation的良好范例。在the documentation for it上,artists参数被提及为“每个列表条目都是艺术家的集合,代表需要在每个框架上启用的艺术家。对于其他框架,这些艺术家将被禁用”。但是,我没有得到确切的期望。我制作了Poly3DCollection,但仅通过将其添加到self.images来传递它还是不够的,因为最后也需要将其添加到axesax.add_collection(pc))中。我认为需要直接添加图像,因此尝试将pc添加到collection并将ax附加到self.images,这最终是完全错误的。

我遇到的大多数错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-bd3b8eac5524> in <module>()
     56     animated_gif.add(step, area=str(area))
     57 
---> 58 animated_gif.save('cell-animated1.gif')

<ipython-input-1-bd3b8eac5524> in save(self, filename)
     42         animation = anim.ArtistAnimation(self.fig, self.images)
     43         print animation
---> 44         animation.save(filename, writer='imagemagick', fps=4)
     45 ##################################################################
     46 endStep = 100

/Users/name/anaconda2/lib/python2.7/site-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
   1055                 for anim in all_anim:
   1056                     # Clear the initial frame
-> 1057                     anim._init_draw()
   1058                 for data in zip(*[a.new_saved_frame_seq()
   1059                                   for a in all_anim]):

/Users/name/anaconda2/lib/python2.7/site-packages/matplotlib/animation.pyc in _init_draw(self)
   1374         # Flush the needed figures
   1375         for fig in figs:
-> 1376             fig.canvas.draw_idle()
   1377 
   1378     def _pre_draw(self, framedata, blit):

AttributeError: 'NoneType' object has no attribute 'canvas'

我根本没有收到错误。我找不到canvas的详细说明。我正在将fig传递给save,所以它不应该是None

0 个答案:

没有答案