没有弹出或显示plt.imshow()和plt.show()的图像 [英] No image pop-up or display for plt.imshow() and plt.show()

查看:796
本文介绍了没有弹出或显示plt.imshow()和plt.show()的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过复制粘贴到我自己的本地脚本中而不是在Jupyter笔记本上运行它来重新创建cocoapi演示脚本.一切正常,并且肯定有图像读取并可以显示,因为我已经使用openCV的imshow()函数对其进行了测试(然后弹出了图像).但是,当我尝试使用plt.imshow()和plt.show()打开图像时,该图像将不会出现.

I was trying to recreate the cocoapi demo script by copy pasting it into my own local script instead of running it on a Jupyter notebook. Everything works fine and there's definitely an image read and can be displayed because I've tested it with openCV's imshow() function (and the image pops up). However, when I tried opening the image with plt.imshow() and plt.show(), the image would not appear.

我上网搜索解决方案,因为他们认为这是后端问题?但是,当我运行matplotlib.get_backend()时,它返回:'TkAgg'.

I went online to search for solution at they suggested its a backend issue? However, when I ran matplotlib.get_backend(), it returned: 'TkAgg'.

我也跑了:sudo apt-get install tcl-dev tk-dev python-tk python3-tk 没有错误也没有问题.

I also ran: sudo apt-get install tcl-dev tk-dev python-tk python3-tk with no errors nor issues.

from __future__ import print_function
from pycocotools.coco import COCO
import os, sys, zipfile
import urllib.request
import shutil
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
...
# load and display image
I = io.imread('%s/images/%s/%s'%(dataDir,dataType,img['file_name']))
plt.axis('off')
plt.imshow(I)
plt.show()

版本
*操作系统:Ubuntu 16.04
* Matplotlib版本:2.2.3
* Matplotlib后端(print(matplotlib.get_backend())):TkAgg
* Python版本:3.5.2

Versions
* Operating system: Ubuntu 16.04
* Matplotlib version: 2.2.3
* Matplotlib backend (print(matplotlib.get_backend())): TkAgg
* Python version: 3.5.2

推荐答案

对此有两种解决方案. 第一个解决方案由@ImportanceOfBeingErnest指出,它是切换后端.解决方案是此线程中声明的

There's two solutions to this. The first solution is kindly pointed out by @ImportanceOfBeingErnest and it is to switch backends. The solution is stated in this thread

正如@ImportanceOfBeingErnest指出的那样,第二种解决方案不太理想,因为它涉及更改源代码.但是,如果由于某种原因第一种方法不起作用,请随时尝试第二种方法.

As @ImportanceOfBeingErnest has pointed out, the second solution is less ideal because it involves changing source code. But if for whatever reasons the first method doesn't work, feel free to try the second one.

第二个解决方案: 当我运行matplotlib.get_backend()时,它返回:'TkAgg',所以我很困惑为什么它仍然无法正常工作.原来它返回了"TkAgg",因为我在终端上做了类似的事情:

Second solution: When I ran matplotlib.get_backend(), it returned: 'TkAgg' so I was confused why it still didn't work. Turns out that it returned 'TkAgg' because I did something like that in terminal:

Python 3.5.2 (default, Nov 23 2017, 16:37:01)  
[GCC 5.4.0 20160609] on linux  
Type "help", "copyright", "credits" or "license" for more information.  
import matplotlib  
matplotlib.get_backend()  

但要保持一致

from pycocotools.coco import COCO

从终端:

This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

在cocoapi/PythonAPI/pycocotools/coco.py文件中,第三个导入行是:

In the cocoapi/PythonAPI/pycocotools/coco.py file, the third import line was:

import matplotlib; matplotlib.use('Agg')

将此更改为:

import matplotlib; matplotlib.use('TkAgg')

一切都会好起来的.

这篇关于没有弹出或显示plt.imshow()和plt.show()的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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