for循环中错误的图表和文本序列

时间:2017-01-03 16:06:18

标签: python matplotlib jupyter

当我在jupyter笔记本中运行以下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>

其中&lt; 1st pic&gt;等等只是占位符代替真实图片,这里没有描述。

显然,预期的输出是

<1st pic>
Some text 

<2nd pic>
Some text 

<3rd pic>
Some text 

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

1 个答案:

答案 0 :(得分:2)

你没有告诉他告诉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