Matplotlib – 使用plt.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_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")

输出是这样的:

看来你正在使用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()

输出:

enter image description here

https://stackoverflow.com/questions/49365763/matplotlib-sequence-is-off-when-using-plt-imshow

转载注明原文:Matplotlib – 使用plt.imshow()时序列关闭