print() & matplitlib.pyplot.imshow() 未在 Jupyter T笔记本'for'循环中同步



我正在尝试打印jupyter笔记本单元格中每个图像对应的文本。下面是一个伪代码块:

#paths = list of image paths; say 10 image-paths
#document_text = list of strings as long as 200 words corresponding each image. say 10 strings
%matplotlib inline
import matplotlib.pylplot as plt
from PIL import Image

for path,text in enumerate(paths,document_text):
print(document_text)
img = Image.open(path)
plt.imshow(img)

代码块工作正常,但有一个小故障。所有打印语句都被打印出来&然后显示所有图像,即10~200个单词串,然后显示10个图像。我想要的是一个打印,然后是相应的图像。如何做到这一点?我不想使用plt.title((,因为字符串很长。

您需要使用plt.show():渲染每个图像

for path,text in enumerate(paths,document_text):
print(document_text)
img = Image.open(path)
plt.imshow(img)
plt.show()

最新更新