for循环中错误的情节和文本顺序

Wrong sequence of plots and texts in a for loop

当我在 jupyter notebook 中 运行 以下 for 循环时

for index,image in enumerate(pics):
   plt.figure(figsize=(1,1))
   plt.imshow(image.squeeze())
   print("Some text ")

我得到了输出

Some text 
Some text 
Some text 

<1st pic>
<2nd pic>
<3rd pic>

其中 <1st pic> 等只是代替真实图片的占位符,此处未显示。

显然,预期的输出将是

<1st pic>
Some text 

<2nd pic>
Some text 

<3rd pic>
Some text 

当我想独立处理文本而不是例如文本时,如何获得预期的输出?作为图片标题?

你不是让他告诉matplotlib给你看图。我假设您在笔记本中使用了 %matplotlib inline,这导致单元格末尾的 plt.show() 变为 运行。

for index in range(3):
    plt.figure(figsize=(1,1))
    plt.show()
    print("Some text ")

输出:

<matplotlib.figure.Figure at 0x7ff844b2b048>
Some text 
<matplotlib.figure.Figure at 0x7ff86cc4a7b8>
Some text 
<matplotlib.figure.Figure at 0x7ff86908dcf8>
Some text