python - 我的 matplotlib ArtistAnimation 做错了什么?

标签 python animation matplotlib

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

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'

最佳答案

我认为问题是您已经创建了 PatchCollections,但尚未将它们添加到轴中。您应该可以使用 ax.add_collection 来执行此操作

尝试将 carCollection 附加到 frames 的行更改为:

frames.append(ax.add_collection(carCollection))

关于python - 我的 matplotlib ArtistAnimation 做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35612263/

相关文章:

python - 将列表中的项目从前移到后

python - 使用 AWS S3 s3fs/boto3 读取 h5 文件

python - 计算数组的索引以生成热图

Android:将动画应用于 View 会创建 RuntimeException

python - y 轴为日期的水平条形图

python - 如何在 Pandas 数据框中的所需索引处插入新行

javascript - 最适合 2d/投影 3d JS 矢量动画的技术/库?

jquery - 使用 jQuery 动画人物跳跃

python - 防止 x 轴标签在 matplotlib/pyplot 中被截断

python - 将图例添加到散点图