首页 > 解决方案 > Matplotlib 后端在使用对象检测 API、QtAgg 到 Agg 时发生变化

问题描述

使用带有对象检测 API 的 matplotlib 时,该图没有显示,但在我导入一些测试方法之前它工作正常。

我正在使用 Anaconda 虚拟环境、python 3.6 和 google 对象检测 API。

import matplotlib.pyplot as plt

import tensorflow as tf

from matplotlib import patches

from object_detection.anchor_generators.multiple_grid_anchor_generator import create_ssd_anchors

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest

变化发生在最后一行

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest"

之前,我可以显示图形,例如 plt.subplot(2,2),我会弹出图形和以下内容:

(<Figure size 640x480 with 4 Axes>,
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f4615eb8ba8>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f4614d320f0>],
    [<matplotlib.axes._subplots.AxesSubplot object at 0x7f4614ce36a0>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f4614c92c50>]],
   dtype=object))
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons

如果您依赖此处未列出的功能,请提出问题。

并且“plt.get_backend()”显示“Qt5Agg”。但是在最后一行之后,该图没有弹出,尽管我确实从“plt.subplots(2,2)”中得到了结果:

(<Figure size 640x480 with 4 Axes>,
 array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f460b8a5f60>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f460b86b5c0>],
    [<matplotlib.axes._subplots.AxesSubplot object at 0x7f460a7b2fd0>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f460a7cb630>]],
   dtype=object))

现在当我输入“plt.get_backend()”时,它显示的是“Agg”,而不是之前的“Qt5Agg”。并且“plt.show()”会引发错误:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  #!/usr/bin/env python2

我使用的口译员是

~/anaconda3/envs/py36/bin/python

这里有环境变化吗?

最后一行的脚本可以在这里找到

非常感谢您的时间!

标签: pythonmatplotlibvirtualenv

解决方案


解决了。在 ~/models/research/object_detecion/utils/visualization_utils.py 中找到了这一行:

import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements

只需评论它并且工作正常。


推荐阅读