【发布时间】:2020-02-01 01:18:03
【问题描述】:
我似乎找不到在 Jupyter Notebook 中使用普通 matplotlib 绘制图像序列的简单快速方法。我尝试过 FuncAnimation、fig.canvas.draw()、blitting,以及标准的 imshow-pause 组合;没有成功或刷新率非常慢。我不需要图像是交互式的——它们只需要按顺序显示,并且不能为每个图像弹出一个新的图形窗口。我在这里看到了很多解决方案,但似乎没有一个能按我想要的方式工作。
我的通用管道会进行大量处理,每个图像都会在一段时间或 for 循环内生成和绘制。 FuncAnimation 是不可取的,因为它需要传递函数句柄,而且我的用例涉及许多参数和状态变量,因此难以使用。
我得到的最好的是下面使用 fig.canvas.draw() 的工作示例 - 显示每次迭代的绘制时间线性增加,我需要它保持不变!
import numpy as np
import matplotlib.pyplot as plt
from timeit import default_timer as timer
%matplotlib notebook
num_iters = 50
im = np.arange(60).reshape((15,4))
fig, ax = plt.subplots(1,1)
fig.show()
fig.canvas.draw()
iter_times = np.zeros(num_iters)
for i in range(num_iters):
im = np.roll( a=im, shift=1, axis=0 )
t0 = timer()
ax.imshow(im.T, vmin=im.min(), vmax=im.max())
ax.set_title('Iter # {}/{}'.format(i+1, num_iters))
fig.canvas.draw()
iter_times[i] = timer()-t0
plt.figure(figsize=(6,3))
plt.plot(np.arange(num_iters)+1, iter_times)
plt.title('Imshow/drawing time per iteration')
plt.xlabel('Iteration number')
plt.ylabel('Time (seconds)')
plt.tight_layout()
plt.show()
【问题讨论】:
标签: matplotlib animation jupyter-notebook