【问题标题】:What am I doing wrong with my matplotlib ArtistAnimation?我的 matplotlib ArtistAnimation 做错了什么?
【发布时间】:2016-02-24 20:20:22
【问题描述】:

我正在尝试使用以下代码创建动画,但无法正常工作。

def plotSimulation(currentState, startState):
   import matplotlib.pyplot as plt
   import matplotlib.animation as ani
   import numpy as np
   import matplotlib.patches as mpatches
   import matplotlib.collections as collections

   fig1 = plt.figure()
   ax = plt.subplot()

   numberOfFrames = currentState.shape[2]


   frames = []

   for frame in np.arange(numberOfFrames):
      carPatches = []
      for car in range(0, currentState.shape[0]):
          carPatches.append(mpatches.Rectangle([currentState[car,0,frame],currentState[car,1,frame]], startState[car, 2], startState[car, 3], angle=startState[car, 4]))
      carCollection = collections.PatchCollection(carPatches)
      frames.append((carCollection,))


   simulationAnimation = ani.ArtistAnimation(fig1, frames, interval=50, repeat_delay=300, blit=False)

   plt.show()

我试图从这个例子中复制http://matplotlib.org/examples/animation/basic_example.html

图2中的动画

但是当我尝试运行我的动画时,我得到:

File "...plotIteration.py", line 24, in plotSimulation
  simulationAnimation = ani.ArtistAnimation(fig1, frames, interval=50, repeat_delay=300, blit=False)

File ".../python/matplotlib/animation.py", line 934, in __init__
  TimedAnimation.__init__(self, fig, *args, **kwargs)

File ".../python/matplotlib/animation.py", line 878, in __init__
  *args, **kwargs)

File ".../python/matplotlib/animation.py", line 559, in __init__
  self._init_draw()

File ".../Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/animation.py", line 948, in _init_draw
  ax.figure.canvas.draw()

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

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    我认为问题在于您已经创建了PatchCollections,但尚未将它们添加到轴上。你应该可以使用ax.add_collection

    尝试将附加carCollectionframes 的行更改为:

    frames.append(ax.add_collection(carCollection))
    

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2021-04-14
      • 2021-04-19
      • 2017-03-13
      • 2017-01-18
      • 2013-06-24
      相关资源
      最近更新 更多