没有图像弹出或显示plt.imshow()和plt.show()

问题描述 投票:1回答:1

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

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

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

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

image matplotlib python-3.5 backend
1个回答
2
投票

这有两个解决方案。第一个解决方案由@ImportanceOfBeingErnest友情指出,它是切换后端。解决方案是stated in this thread

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

第二个解决方案:当我运行matplotlib.get_backend()时,它返回:'TkAgg'所以我很困惑为什么它仍然无效。事实证明它返回'TkAgg'因为我在终端做了类似的事情:

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文件中,第三个导入行是:

import matplotlib; matplotlib.use('Agg')

将其更改为:

import matplotlib; matplotlib.use('TkAgg')

事情应该没问题。

© www.soinside.com 2019 - 2024. All rights reserved.