【问题标题】:Message "Matplotlib is currently using agg" and Matplotlib doesn't display image消息“Matplotlib 当前正在使用 agg”并且 Matplotlib 不显示图像
【发布时间】:2019-07-02 08:19:38
【问题描述】:

其他人也有这个问题,我用了他们的解决方案但没有解决。

我使用virtual env with python3.5Matplotlib 安装在 virtual env 下。 我在系统上安装了 python3.tkinter。

当我检查时

matplotlib.get_backend()

我有

>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'

但是当我运行下面的代码时

for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)
  #plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)
  plt.show()

我有问题

 UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  % get_backend())

我把标题作为

from io import StringIO
import matplotlib
matplotlib.rcParams["backend"] = "TkAgg"
from matplotlib import pyplot as plt
from PIL import Image

有人说已经解决了,但我还是有同样的问题,plt 不显示图片。

【问题讨论】:

  • 你的警告不是很清楚吗?您需要使用 GUI 类型的后端来获取带有show() 的图像。你在什么系统上?
  • Ubuntu 16.04。这是警告。但问题是图像不显示。
  • 看看this post是否有帮助,尤其是显示如何找出可用后端的部分。其中,this post 展示了如何更改当前后端。
  • 你如何执行你的程序?哪个python命令? which python3 的结果是什么?如果将 matplotlib.rcParams["backend"] = "TkAgg" 替换为 matplotlib.use("TkAgg") 会怎样?
  • @PierredeBuyl which python3 给了我/home/cnv/venvpy3_cpu/bin/python3.matplotlib.use("TkAgg") 仍然有相同的警告并且没有图像显示。

标签: python matplotlib tkinter


【解决方案1】:

作为you noted,Matplotlib 后端为sometimes require extra steps to run in virtual environments

话虽如此,上面链接的文档也表明 TkAgg 应该可用:

[...] Tk 框架(TkAgg 后端)不需要任何外部依赖项,通常始终可用。

我使用 Ubuntu,我认为 TkAgg 将依赖 PyGObject。该选项本身有一个注释,链接到构建说明。

跟着@​​987654323@,我去安装它的系统dependencies

sudo apt-get install -y python3-venv python3-wheel python3-dev
sudo apt-get install -y libgirepository1.0-dev build-essential \
  libbz2-dev libreadline-dev libssl-dev zlib1g-dev libsqlite3-dev wget \
  curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libcairo2-dev

然后将以下Python dependencies添加到我项目的虚拟环境中:

  • pycairo
  • pygobject
# inside my project's virtual environment
pip install pycairo
pip install pygobject

完成后,像往常一样运行我的项目会显示预期的图表。

注意事项

  • 我在项目的虚拟环境中使用 Ubuntu 18.04.2 和 Python 3.6.8。

  • 我跳过了 PyGObject 的大部分构建说明,只执行了我上面描述的操作。

【讨论】:

    【解决方案2】:

    在我的情况下,我通过用 PIL Image 函数替换 pyplot 函数来解决它:

    (我的猜测是某些图像格式的功能出错)

    import matplotlib
    matplotlib.use('GTK3Agg')
    import matplotlib.pyplot as plt
    
    from PIL import Image
    
    
    # plt.imshow(image_np)
    # plt.show()
    img = Image.fromarray(image_np, 'RGB')
    img.show()
    

    【讨论】:

      【解决方案3】:

      在运行时尝试将其添加到您的代码中:

      %matplotlib inline
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 2020-04-30
      • 2019-08-21
      相关资源
      最近更新 更多