5

i'm getting the following error message, when trying to call a pyplot.show.

> QObject::moveToThread: Current thread (0x55cb11264ad0) is not the object's thread (0x55cb1226c700).
> Cannot move to target thread (0x55cb11264ad0)

> qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
> This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

> Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx.

> Aborted (core dumped)

Looking for answers I though it was the same problem as https://www.reddit.com/r/archlinux/comments/d6h3kl/python_qt_cant_import_matplotlibpyplot/ but i just reinstalled a lot of packages(And almost broke the entire system) and Im getting the same error.

And also, matplotlib.pyplot.show() and opencv.imshow() works fine when they are alone, but when i am doing some procesing with opencv and i want to show in pyplot.show() is where the error comes out.

I don't know how to debug Qt as the same as u/tim-hilt done in his post so this is all information that i can give for the moment. I figured out how to do it.

https://pastebin.com/YAgHMQKf

Thanks in advance for any response that i can get.

5
  • This question deals with a similar issue : stackoverflow.com/questions/52337870/… Commented Aug 15, 2020 at 8:43
  • opencv and matplot can use different GUI framework and they may have problem to run together - they may need own event loop which can make problem. threading could resolve problem but usually GUIs don't like change widgets in different thread. Maybe convert plot to image and use only opencv to display it. OR create own GUI to display different object and have only one event loop i Commented Aug 15, 2020 at 9:20
  • But it worked a week ago, the problem is that i need to plot graphs and also images so the solution was to use only matplotlib to show them, but now it is not working. Commented Aug 15, 2020 at 12:46
  • I have used opencv with matplotlib and I use arch linux and I have not had problems, so you should provide a minimal reproducible example and point out how you have installed the libraries Commented Aug 15, 2020 at 15:12
  • Well I looks like its more complicated, I have an environment for the project and it just works, so, the real question is how many packages in my arch linux installation are broken? Literally I reinstalled almost any package in my current installation and It doesn't works with the main python of the system environment. If someone can tell me any ideas for posting here for give more information I would be glad to do it. Commented Aug 16, 2020 at 5:41

2 Answers 2

2

You can import matplotlib and make it use tkagg:

import numpy as np
import matplotlib

matplotlib.use("tkagg")
import matplotlib.pyplot as plt
import cv2

Worked at least at my case.

Sign up to request clarification or add additional context in comments.

Comments

1

This issue happens due to a conflict between GUI backend dependencies of opencv-python and matplotlib. If you are not using cv2.imshow(), then you can simply remove opencv-python and install rather opencv-python-headless which installs the package without any GUI dependency and this removes the conflict with matplotlib. That solved my problem. Just run the following on the terminal:

pip uninstall opencv-python
pip uninstall matplotlib
pip uninstall PyQt5

pip install matplotlib
pip install PyQt5
pip install opencv-python-headless

If you are using a conda environment, I'd suggest to scrap it. Create instead a new virtualenv environment and you can put it at the same location as the conda created ones maybe. Conda created environments create havoc with GUI backends. And yes, use an older version of Python while creating the new environment. For me Python 3.8 worked while the latest, i.e., Python 3.11 didn't.

This issue is also described here.

3 Comments

If the answer is already posted in another Stack Overflow question, the correct course of action is to flag the question as a duplicate, not post the same or very similar answer to multiple places. Also demanding that users upvote your posts is not appropriate.
OK. Sorry I didn't know that demanding an upvote is unethical. I just wanted to make sure that people don't turn away from this solution since it works. Also, I was the one who posted the question on the other stack (link# 1) and I was the one who found the solution and answered the question. Should I flag my own question as duplicate or this one?
This question is older and has more views and votes, so I suggest you keep this answer on this question, and flag yours as a duplicate of this one. I would also suggest ahead and delete your answer over there on the newer questions so there will not be 2 of them. People will up/down vote your answer here as they see fit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.