我似乎找不到一种在 Jupyter Notebook 中使用普通 matplotlib 绘制图像序列的简单快速的方法。我尝试过 FuncAnimation、fig.canvas.draw()、blitting,以及标准的 imshow-pause 组合;没有成功或刷新率非常低。我不需要图像是交互式的——它们只需要按顺序显示,并且不能为每个图像弹出一个新的图形窗口。我在这里看到了很多解决方案,但似乎没有一个按我想要的方式工作。
我的通用管道进行了大量处理,每个图像都在 while 或 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()
我认为问题在于情节正在“建立”,因此每次都在绘制每个情节。如果您在ax.clear()
之前添加imshow()
,您将获得线性绘图时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句