Matplotlib-使用plt.imshow()时,序列处于关闭状态

Matplotlib-使用plt.imshow()时,序列处于关闭状态,matplotlib,sequence,imshow,Matplotlib,Sequence,Imshow,我在Jupyter笔记本上写了一个狗分类器,每次在图像中检测到狗时,它都会显示图像并打印一些描述它的文本。不知何故,无论我将plt.imshow()和print()放在哪个顺序,在打印完所有文本后,图像总是会显示出来。有人知道为什么会这样吗 谢谢大家! 以下是我的代码片段: for i in range (0, 1,1): all_counter+=1 if dog_detector(dog_files_short[i]): img = image.load

我在Jupyter笔记本上写了一个狗分类器,每次在图像中检测到狗时,它都会显示图像并打印一些描述它的文本。不知何故,无论我将
plt.imshow()
print()
放在哪个顺序,在打印完所有文本后,图像总是会显示出来。有人知道为什么会这样吗

谢谢大家!

以下是我的代码片段:

for i in range (0, 1,1):

    all_counter+=1

    if dog_detector(dog_files_short[i]):

        img = image.load_img(dog_files_short[i], target_size=(224, 224))
        plt.show()
        plt.imshow(img)
        time.sleep(5)
        print("That's a dog!!!!")
        dog_counter+=1
        print("______________")

    else: 

        print("______________")
        img = image.load_img(dog_files_short[i], target_size=(224, 224))
        plt.show()
        plt.imshow(img)
        print("No Doggo up here :(")
        print(ResNet50_predict_labels(dog_files_short[i]))
        print("______________")

print((dog_counter/all_counter)*100, "% of the dog pictures are classified as dogs")
输出如下:


我在我的ipython笔记本中尝试了这一点,如果我在第一次获得图像和文本后立即调用plt.imshow(img)和plt.show()。

您似乎正在使用Juypter笔记本。这始终显示输出中最后一个自动生成的输出(如matplotlib图)

您可以使用
IPython.display.display
在图形所属的输出位置显示图形

import matplotlib.pyplot as plt
import numpy as np
from IPython.display import display

images = [np.random.rayleigh((i+1)/8., size=(180, 200, 3)) for i in range(4)]

dog_detector = lambda x: np.random.choice([True,False])
dog_counter = 0

for i in range(len(images)):

    if dog_detector(images[i]):
        dog_counter+=1
        fig, ax = plt.subplots(figsize=(3,2))
        ax.imshow(images[i])
        display(fig)
        display("That's a dog!!!!")
        display("______________")

    else: 

        display("______________")
        fig, ax = plt.subplots(figsize=(3,2))
        ax.imshow(images[i])
        display(fig)
        display("No Doggo up here :(")
        display("______________")

perc = (dog_counter/float(len(images)))*100 
display("{}% of the dog pictures are classified as dogs".format(perc))
plt.close()
输出: