31

On my OS(Linux Mint Debian Edition 2), except for the system python(/usr/bin/python) installed by the apt, I also installed the anaconda. But I've encounterd a problem running the following code with the anaconda python

# test.py
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1])
plt.scatter(x, x)
plt.show()

The error is

This application failed to start because it could not find or load the Qt platform plugin "xcb".

Reinstalling the application may fix this problem.

Aborted

But if I try with the system python, i.e., /usr/bin/python test.py, it works correctly.

Then I tried the ipythons, of system and of anaconda, the result is same as before: the anaconda ipython kernel died.

And I tried add the ipython magic %matplotlib inline into the code, the anaconda ipython works correctly now. But if I replace the %matplotlib inline with %pylab, the anaconda ipython died again.

Note: I use the python 2.7. System ipython's version is 2.3, anaconda ipython's version is 3.2.

user1251007
  • 15,891
  • 14
  • 50
  • 76
Yantao Xie
  • 12,300
  • 15
  • 49
  • 79
  • I also tried install the *xcb*, `conda install --channel https://conda.anaconda.org/jdreaver libxcb`, but the problem is as before. – Yantao Xie Oct 10 '15 at 08:10
  • 2
    The underlying problem is probably that the system Qt and the anaconda Qt are missmatched and the plugin system (which works via ENVs) is getting crossed. The activate script at https://gist.github.com/tacaswell/1d36a5ecc372f65e9f46 might be of some help. When I was on a KDE box I used than regularly. – tacaswell Oct 12 '15 at 02:13
  • I read the [conda doc](http://conda.pydata.org/docs/) carefully. And I changed my env setting to avoid to mix the python packages system installed or pip installed with the anaconda packages. Then I reinstall the anaconda. I find the problem is disapearing. – Yantao Xie Oct 13 '15 at 09:05
  • @tcaswell I read the script and tried it, there is not effect. – Yantao Xie Oct 13 '15 at 09:05

6 Answers6

33

To fix this problem, I added to the top:

import matplotlib
matplotlib.use('Agg')

I have a CentOS7, Anaconda3-4.5.0, python3.5 pyqt version 5.6.0 and qt version 5.6.0

If you want to use the plot in a Tkinter window for visualisation then use:

matplotlib.use('TkAgg')
Community
  • 1
  • 1
Scott Young
  • 331
  • 3
  • 3
  • 2
    This solution works when a qt application calls a python script that uses matplotlib. – Alechan Oct 04 '18 at 20:08
  • Seemed to work for me, I was trying to generate some graphs via an ssh session. Only using plt.savefig(), not imshow() or show(). – Ketil Malde Apr 25 '22 at 09:34
21

Same problem with Linux Mint 17, 64 bit. It was solved after 4h searching on the net! You need to give these commands on the terminal from folder /anaconda2/bin

sudo ./conda remove qt
sudo ./conda remove pyqt
sudo ./conda install qt
sudo ./conda install pyqt

Hope it helps!

anto150192
  • 539
  • 3
  • 13
  • 2
    After trying this with anaconda 4.3.21. for python 2.7 it uninstalled various important packages (`jupyter`, `matplotlib`, etc.), not only `qt`. This solution did not work for me. I am trying to run this on Rstudio and I'm getting the same error `This application failed to start because it could not find or load the Qt platform plugin "xcb".` – Luís Telles May 26 '17 at 16:31
  • I did reboot, I did reinstall the packages and still the same thing. I even installed xcb and libxcb1 libxcb-util1 libxcb-util-dev, still nothing.. This happens with Anaconda and Ubuntu 16.04 4.10.0-21-generic python. – Daniel Colceag Jun 02 '17 at 23:46
  • This worked for me, but only after I followed it up with a `./pip uninstall matplotlib; ./pip install matplotlib` – R.M. Sep 06 '18 at 20:00
8

I experienced this problem on Ubuntu 16.04 with anaconda 4.3.17 (Python 2.7). The issue stemmed from anaconda having Qt version 5.6 installed, while my system Qt libraries were at version 5.5.

A quick hack is to make sure Anaconda libraries precede your system libraries by setting LD_LIBRARY_PATH:

export LD_LIBRARY_PATH=$HOME/anaconda2/lib:$LD_LIBRARY_PATH

Unfortunately, this will break other programs that use Qt 5.5, so you can only use it in situations that are 100% anaconda python, for example, if you're starting an ipython session with --pylab.

I discovered that this was the issue by looking at the way libxqcb.so was linked:

ldd $HOME/anaconda2/plugins/platforms/libqxcb.so

which reported the following errors:

./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5: version `Qt_5' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.6' not found (required by ./libqxcb.so)

The error messages are saying they can't find Qt_5.6, which is anaconda's version. The version on my system was 5.5, which I found out by looking at the filenames that resulted from this command:

ls /usr/lib/x86_64-linux-gnu/libQt*
goats
  • 101
  • 1
  • 3
  • this solution is temporary? I mean, it's simply running `export LD_LIBRARY_PATH=$HOME/anaconda2/lib:$LD_LIBRARY_PATH` on a terminal and when I close the terminal the variable `LD_LIBRARY_PATH` will go back to normal with no risks of affecting other applications? – Luís Telles May 26 '17 at 16:35
  • I use this solution to start pycharm or ipython - I wrote a script that first exports LD_LIBRARY_PATH as above and then executes ipython --pylab, and another script that does the same for pycharm. If I run a python script from the command line that makes plots in matplotlib, I'll export LD_LIBRARY_PATH as above in that situation too. I don't want non-python applications to use anacona's Qt version. – goats May 28 '17 at 18:05
6

This can also happen when using something like an ssh session to connect to for instance a piece of software that is using X11 graphical libraries as I recently discovered.

The issue occurs when trying to plot images in the graphical user interface, using:

cv2.imshow() 

or

plot.show()

In this case you should either use ssh -XY or you should consider saving to a file instead such as

cv2.imwrite('file.png')

or

plt.savefig('file.png')

I had this problem when using a deep neural network that was originally geared towards being used in conjunction with Jupyter notebook. When writing the images to files instead of displays, the code the ran through multiple ssh tunnels without reporting the xcb and Qt bug.

By the way, it didn't matter what variant of Qt4 or Qt5 was installed. It still failed with the same error message.

Eamonn Kenny
  • 1,926
  • 18
  • 20
  • I had this part in my code ```cv2.imshow('image', image), cv2.destroyAllWindows() ``` Then i ran my code in crontab, and got the error above. Only your comment has been useful. – Rivered Jun 01 '23 at 22:15
1

I had to upgrade several python packages to make it work. In particular:

pip3 install --upgrade pyside2 pyqt5

levzettelin
  • 2,600
  • 19
  • 32
0

I had a similar problem. Uninstalling matplotlib with conda and installing again with pip solved the issue. I suspect this has something to do with the opencv version being installed with pip, and them both trying to use qt backends, but I am not certain.

conda uninstall matplotlib
pip install matplotlib
GattacaCat
  • 71
  • 1
  • 2