消息“Matplotlib当前正在使用agg”并且Matplotlib不显示图像

1wnzp6jl  于 2023-04-21  发布在  其他
关注(0)|答案(4)|浏览(130)

别人有这个问题,我用了他们的解决方案却没有解决。
我使用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不显示图像。

83qze16e

83qze16e1#

正如您所注意到的,Matplotlib后端为sometimes require extra steps to run in virtual environments
也就是说,上面链接的文档也表明TkAgg应该可用:
[...]Tk框架(TkAgg后端)不需要任何外部依赖性,通常总是可用的。
我使用的是Ubuntu,我假设TkAgg将依赖于PyGObject。该选项本身有一个链接到构建说明的注解。
PyGObject build instructions之后,我安装了它的系统依赖项:

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依赖项添加到项目的虚拟环境中:

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

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

注意事项

  • 我在项目的虚拟环境中使用Ubuntu 18.04.2和Python 3.6.8。
  • 我跳过了PyGObject的大部分构建指令,只做了上面描述的内容。
hfyxw5xn

hfyxw5xn2#

在我例子中,我通过将pyplot函数替换为PIL Image函数来解决它:
(my我猜是某些图像格式的函数出错了)

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()
lokaqttq

lokaqttq3#

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

%matplotlib inline
slwdgvem

slwdgvem4#

对于Windows,请确保在安装

期间选中此复选框

相关问题