python - 消息 "Matplotlib is currently using agg"和 Matplotlib 不显示图像

标签 python matplotlib tkinter

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

我用 virtual env with python3.5 .Matplotlib安装在 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一样的问题不显示图像。

最佳答案

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

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

[...] the Tk framework (TkAgg backend) does not require any external dependencies and is normally always available.



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

关注 PyGObject build instructions ,我去安装它的系统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 的大部分构建指令,只做了我上面描述的。
  • 关于python - 消息 "Matplotlib is currently using agg"和 Matplotlib 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586504/

    相关文章:

    python - Tkinter 菜单命令目标函数带参数?

    python - 从字符串中删除 "\n",但保留实际的换行符?

    python - 将 CSV 文件写入 PDF 文档

    python - PyQt5 QWebEngine 禁用 Access-Control-Allow-Origin

    python - 在 Python 中跟踪一个被忽略的异常?

    matplotlib - 在 matplotlib 中用圆裁剪三角形

    python - Django ManyToMany 关系 add() 错误

    python - 使用 Matplotlib 在绘图上叠加旋转图像

    python - python 中的系数图

    python - 获取 Tkinter 窗口大小