我一直在尝试优化我的matplotlib动画,这包括在每次迭代时为该图添加一个矩形。由于动画功能会在每次迭代时更新整套修补程序,因此大量修补程序的速度非常慢。Python Matplotlib:PatchCollection动画不会更新
根据多个消息来源,这个过程可以通过使用PatchCollection而不是循环补丁来加快。这是我的尝试:
from matplotlib import pyplot
from matplotlib import patches
from matplotlib import animation
from matplotlib.collections import PatchCollection
fig = pyplot.figure()
coords = [[1,0],[0,1],[0,0]] #arbitrary set of coordinates
ax = pyplot.axes(xlim=(0, len(coords)), ylim=(0, len(coords)))
ax.set_aspect('equal')
patchList = list()
for coord in coords:
patch = patches.Rectangle(coord, 1, 1, color="white")
ax.add_patch(patch)
patch.set_visible = True
patchList.append(patch)
rectangles = PatchCollection(patchList, animated=True)
ax.add_collection(rectangles)
black = []
white = ["white"]*len(patchList)
def animate(i):
black.append("black")
white.pop()
colors = black + white
print(colors)
rectangles.set_facecolors(colors)
print("%.2f%%..."%(100*float(i+1)/len(coords)))
return rectangles,
anim = animation.FuncAnimation(fig, animate,
# init_func=init,
frames=len(coords)-1,
interval=1,
blit=True,
repeat = False)
pyplot.show()
动画从不更新。它被冻结在一个空的地块上。
我会对其他优化方式开放。目前的解决方案似乎相当冗长,只是每个框架添加一个矩形。
谢谢,这解决了我的问题。额外的add_patch行是我以前使用的方法的遗迹,它没有使用PatchCollections类,所以我只是忘了删除它。 – Ryan
以前,更新地块的实际时间是限制因素。我正在为500-1000个正方形制作动画,所以我希望它尽可能快地运行。 20ms将是一个50fps的动画,这对我的目的很有帮助。 另外,我同意在颜色列表中替换第i个值更有意义。如果您要编写自己的这种风格的动画,您会使用PatchCollections并编辑颜色数组,还是以另一种方式做它? – Ryan
我没有处理大量元素的动画处理经验。但据我所知,只要您可以接受使用“集合”对象的限制,它总是比绘制单个补丁更快 –