plt.imshow() 和 plt.show() 没有图像弹出或显示
No image pop-up or display for plt.imshow() and plt.show()
我试图重新创建 cocoapi 演示脚本,方法是将其复制粘贴到我自己的本地脚本中,而不是 运行 在 Jupyter 笔记本上。一切正常,并且肯定有图像读取并可以显示,因为我已经使用 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()
版本
* 操作系统:Ubuntu16.04
* Matplotlib 版本:2.2.3
* Matplotlib 后端(print(matplotlib.get_backend())
):TkAgg
* Python 版本:3.5.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')
一切都会好起来的。
我试图重新创建 cocoapi 演示脚本,方法是将其复制粘贴到我自己的本地脚本中,而不是 运行 在 Jupyter 笔记本上。一切正常,并且肯定有图像读取并可以显示,因为我已经使用 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()
版本
* 操作系统:Ubuntu16.04
* Matplotlib 版本:2.2.3
* Matplotlib 后端(print(matplotlib.get_backend())
):TkAgg
* Python 版本:3.5.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')
一切都会好起来的。