下面我有一些自包含的示例 matplotlib 动画代码。它运行良好,但在我运行 ani.save
时非常慢——前 100 帧需要大约 18 秒。
我的实际数据 unique_iterable_array
的列表长度约为 20,0000 个元素。我正在尝试每帧打印该列表的 1 个元素。
出于本示例的目的,我仅用随机抽取替换了实际数据,因此代码很容易运行。
unique_iterable_array = [np.random.random_sample((16,4)).round(1) for i in range(1000)]
###################
# make the plot
###################
va = np.zeros((16,4)).round(1)
#colorarray
va_color = np.zeros((16,4))
fig = plt.figure(figsize=(4,9))
def add_axes_inches(fig, rect):
w,h = fig.get_size_inches()
return fig.add_axes([rect[0]/w, rect[1]/h, rect[2]/w, rect[3]/h])
axwidth = 3.
cellsize = .5
axheight = 8
#make the squares
ax_va = add_axes_inches(fig, [cellsize, cellsize, axwidth, axheight])
im_va = ax_va.imshow(va_color, vmin=0., vmax=1.3, cmap="Blues")
###This does the actual plotting
for i in range(va.shape[0]):
for j in range(va.shape[1]):
ax_va.text(j,i, va[i,j], va="center", ha="center")
def animate(current_np):
fig.clear()
ax_va = add_axes_inches(fig, [cellsize, cellsize, axwidth, axheight])
im_va = ax_va.imshow(va_color, vmin=0., vmax=1.3, cmap="Blues")
for i in range(va.shape[0]):
for j in range(va.shape[1]):
ax_va.text(j, i, current_np[i, j], va="center", ha="center")
ani = FuncAnimation(fig, animate, frames=unique_iterable_array[0:100], interval =50)
start_time = time.time()
ani.save('q_change.mp4')
end_time = time.time()
print(end_time -start_time)