Matplotlib funcanimation blit slow
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5
--
Chapters
00:00 Matplotlib Funcanimation Blit Slow
01:52 Accepted Answer Score 4
02:29 Thank you
--
Full question
https://stackoverflow.com/questions/1984...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5
--
Chapters
00:00 Matplotlib Funcanimation Blit Slow
01:52 Accepted Answer Score 4
02:29 Thank you
--
Full question
https://stackoverflow.com/questions/1984...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib
#avk47
ACCEPTED ANSWER
Score 4
Update 600 rectangles every frame is very slow, cbar_blit mode in your code is faster because you only update the rectangles which's color is changed. You can use PatchCollection to speedup drawing, here is the code:
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import matplotlib.animation as manim
from matplotlib.collections import PatchCollection
Nx = 30
Ny = 20
numtimes = 100
size = 0.5
x, y = np.ogrid[-1:1:30j, -1:1:20j]
data = np.zeros((numtimes, Nx, Ny))
for i in range(numtimes):
data[i] = (x-i*0.02+1)**2 + y**2
colors = plt.cm.rainbow(data)
fig, ax = plt.subplots()
rects = []
for (i,j),c in np.ndenumerate(data[0]):
rect = plt.Rectangle([i - size / 2, j - size / 2],size, size)
rects.append(rect)
collection = PatchCollection(rects, animated=True)
ax.add_collection(collection)
ax.autoscale_view(True)
def animate(tind):
c = colors[tind].reshape(-1, 4)
collection.set_facecolors(c)
return (collection,)
ani = manim.FuncAnimation(fig, animate, frames=numtimes,
interval=10, blit=True, repeat=False)
plt.show()