我的matplotlib ArtistAnimation出了什么问题?

2024-09-26 17:51:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个动画使用以下代码,但我不能让它工作。在

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显示的动画

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

^{pr2}$

Tags: importframesmatplotlibasnp动画pltcar
1条回答
网友
1楼 · 发布于 2024-09-26 17:51:42

我认为问题是你已经创建了你的PatchCollections,但没有将它们添加到轴上。您应该能够使用^{}完成此操作

尝试将carCollection附加到frames的行改为如下内容:

frames.append(ax.add_collection(carCollection))

相关问题 更多 >

    热门问题