如何在IPython循环期间内联显示ndarray图像?

2024-05-15 18:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码

%matplotlib inline

for i in range(0, 5):
    index = np.random.choice(len(dataset))
    print('index:', index)
    image = dataset[index, :, :]
    print('image shape:', np.shape(image))
    plt.imshow(image)

jupyter notebook结尾显示五个打印输出和一个图像。

是否可以在每次循环迭代中显示图像?

我可以用图像文件

for fullname in fullnames:
  print('fullname:', fullname)
  display(Image(filename=fullname))

对ndarrays也可以这样做吗?

更新

写作

for i in range(0, 5):
   ...
   plt.figure()
   plt.imshow(image)

使它更好,但不是完美的。图像显示为多个,但都在文本之后。

应该是交错的。


Tags: 代码in图像imageforindexmatplotlibnp
1条回答
网友
1楼 · 发布于 2024-05-15 18:27:05

尝试:

for i in range(0, 5):
   ...
   plt.figure()
   plt.imshow(image)
   plt.show()

如果没有plt.show(),图形只在单元格完成执行后呈现和显示(即退出for循环)。使用plt.show()可以在每次迭代后强制呈现。

相关问题 更多 >