Matplotlib-使用plt.imshow()时序列关闭 [英] Matplotlib - sequence is off when using plt.imshow()

查看:409
本文介绍了Matplotlib-使用plt.imshow()时序列关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jupyter笔记本中编写了一个狗分类器,每次在图像中检测到狗时,都应该显示该图像并打印一些描述它的文本.无论如何,无论我按plt.imshow()print()的顺序排列,总是在打印完所有文本后始终显示图像.有人知道为什么会这样吗?

I write a dog-classifier in a Jupyter notebook that, every time a dog is detected in an image, should show the image and print some text describing it. Somehow, the images are always displayed after all the text was printed, no matter in which order I put plt.imshow() and print(). Does anybody know why this is the case?

谢谢!

这是我的代码段:

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数字).

It seems you are using Juypter notebook. This always shows any autogenerated output (like the matplotlib figures) last in the output.

您可以使用IPython.display.display在输出所属的位置显示这些数字.

You may use IPython.display.display to display the figures at the position of the output where they belong.

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()

输出:

这篇关于Matplotlib-使用plt.imshow()时序列关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆