对此有两种解决方案。
@ImportanceOfBeingErnest 善意地指出了第一个解决方案,它是切换后端。解决办法是在这个线程中说明 https://stackoverflow.com/questions/3285193/how-to-switch-backends-in-matplotlib-python
正如 @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')
一切都应该没问题。