【问题标题】:print() & matplitlib.pyplot.imshow() not synced in 'for' loop in jupyter notebookprint() 和 matplitlib.pyplot.imshow() 未在 jupyter notebook 的“for”循环中同步
【发布时间】:2021-01-06 05:26:40
【问题描述】:

我正在尝试打印与 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() 因为字符串很长。

【问题讨论】:

    标签: python for-loop matplotlib printing jupyter-notebook


    【解决方案1】:

    你需要用plt.show()渲染每张图片:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多