【问题标题】:Fast image sequences / animation in Jupyter Notebook with matplotlib使用 matplotlib 在 Jupyter Notebook 中快速图像序列/动画
【发布时间】: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


    【解决方案1】:

    我认为问题在于情节正在“建立”,因此每次都在绘制每个情节。如果您在 imshow() 之前添加 ax.clear(),您将获得线性绘图时间。

    【讨论】:

    • 我发誓我之前尝试过 - 但我想这个精简的例子不会。加快速度是另一个我相信已经在其他地方得到解答的问题。谢谢!
    • 我以前看过以下内容,但需要一个纯 matplotlib 版本。 holoviews.org/user_guide/Plotting_with_Matplotlib.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    相关资源
    最近更新 更多