2

Other people have this problem, I used their solutions but not solved.

I use virtual env with python3.5. Matplotlib is installed under virtual env. I have python3.tkinter installed at system.

When I check

matplotlib.get_backend()

I have

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

But when I run the code below

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

I have problem as

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

I put at header as

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

some people said it was solved, but I still have the same problem and plt doesn't display image.

batuman
  • 6,072
  • 17
  • 79
  • 178
  • Isn't the warning you get pretty clear? You need to use a GUI-type backend to get an image with `show()`. What system are you on? – Thomas Kühn Feb 08 '19 at 06:27
  • Ubuntu 16.04. This is warning. But the problem is image not displayed. – batuman Feb 08 '19 at 07:06
  • Have a look if there is any help in [this post](https://stackoverflow.com/a/7534680/2454357), especially the part that shows how to find out which backends are available. Among many others, [this post](https://stackoverflow.com/a/20249447/2454357) shows how to change the current backend. – Thomas Kühn Feb 08 '19 at 07:15
  • How do you execute your program? Which python command? What is the result of `which python3`? What if you replace `matplotlib.rcParams["backend"] = "TkAgg"` by `matplotlib.use("TkAgg")` ? – Pierre de Buyl Feb 08 '19 at 07:39
  • @PierredeBuyl `which python3` gives me `/home/cnv/venvpy3_cpu/bin/python3`.`matplotlib.use("TkAgg")` still has same warning and no image display. – batuman Feb 08 '19 at 08:41
  • Did you follow the first link by Thomas Kühn? What does `print(rcsetup.all_backends)` return? – Pierre de Buyl Feb 08 '19 at 08:51
  • @PierredeBuyl Yes I followed. And found out that, `pip installation` inside `venv` doesn't make complete installation. So I need to remove `pip installation` and install again with `apt-get`. Then it is solved. I can display image now. – batuman Feb 09 '19 at 00:24
  • @ThomasKühn thank you – batuman Feb 09 '19 at 00:25

2 Answers2

2

As you noted, Matplotlib backends sometimes require extra steps to run in virtual environments.

That being said, the documentation linked above also indicates that TkAgg should be available:

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

I use Ubuntu, and I assumed TkAgg would be relying on PyGObject. That option has a note itself that links to build instructions.

Following the PyGObject build instructions, I went to install its system 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

Then added the following Python dependencies to my project's virtual environment:

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

Once that done, running my project as usual displayed the expected graph.

Notes

  • I'm using Ubuntu 18.04.2 and Python 3.6.8 in the virtual environment of my project.

  • I skipped through most of the build instructions for PyGObject and only did what I described above.

1

in my case, i solved it by replace pyplot function with PIL Image function:

(my guess is function went wrong for some image format)

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()
sailfish009
  • 1,790
  • 1
  • 17
  • 26