小编use*_*971的帖子

我如何动态(循环)在 google colab 中显示图像?

我一直在尝试使用 pyplot/matplotlib 来显示图像,因为它们在循环中发生变化,但我无法使任何工作正常进行。我基本上无法更新显示的图像。这是复制问题的代码:

f = plt.figure(1)
ax = plt.gca()
show_obj= ax.imshow(np.random.randn(28,28))
for i in range(10):
  print(i)
  # None of these 3 options work
  if True:
    # the image does not update
    show_obj.set_data(np.random.randn(28,28))
    f.canvas.draw()
  if False:
    # image does not update
    ax.clear()
    ax.imshow(np.random.rand(28,28))
    pass
  if False:
    # starts drawing new axes
    f = plt.figure(1)
    ax = plt.gca()
    ax.imshow(np.random.rand(28,28))
  plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib google-colaboratory

7
推荐指数
1
解决办法
4329
查看次数

标签 统计

google-colaboratory ×1

matplotlib ×1

python ×1