8

I am trying to write a PyQt5 application that embeds a matplotlib plot within it. However, I am having a maddening time where if I install matplotlib PyQt5 breaks due to the interference by PyQt4. This can be seen in this error:

In [2]: from PyQt5 import QtCore, QtGui, QtWidgets
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-43848d5bd21e> in <module>()
----> 1 from PyQt5 import QtCore, QtGui, QtWidgets

RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

If I remove PyQt4 (and reinstall PyQt5 since removing PyQt4 removes sip) I then have this issue:

In [1]: import matplotlib.backends.backend_qt5agg
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-6d2c21e1d629> in <module>()
----> 1 import matplotlib.backends.backend_qt5agg

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5agg.py in <module>()
     16
     17 from .backend_agg import FigureCanvasAgg
---> 18 from .backend_qt5 import QtCore
     19 from .backend_qt5 import QtGui
     20 from .backend_qt5 import FigureManagerQT

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py in <module>()
     29     figureoptions = None
     30
---> 31 from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__
     32 from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
     33

C:\Anaconda3\lib\site-packages\matplotlib\backends\qt_compat.py in <module>()
     89     if QT_API in [QT_API_PYQT, QT_API_PYQTv2]:  # PyQt4 API
     90
---> 91         from PyQt4 import QtCore, QtGui
     92
     93         try:

ImportError: cannot import name 'QtCore'

I have gone through this cycle multiple times, installing each from different sources. I am using the Anaconda distribution of Python 3.4, which I have also uninstalled / reinstalled once already.

I must be doing something wrong, but I honestly can't figure out what it is.

Any assistance would be appreciated

dan_g
  • 2,712
  • 5
  • 25
  • 44

2 Answers2

11

As your matplotlib depends on PyQt4, you need to force Matplotlib to use PyQt5 backend. Like this:

import matplotlib
matplotlib.use("Qt5Agg")

This function must be called before importing pyplot for the first time; or, if you are not using pyplot, it must be called before importing matplotlib.backends.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ufbycd
  • 126
  • 1
  • 3
0

The problem is that Matplotlib Uses QT5 and you are using QT4. To solve the problem, you can force Matplotlib to use QT4, by editing matplotlibrc, which is situated in \Lib\site-packages\matplotlib\mpl-dataenter image description here

belkacem mekakleb
  • 584
  • 2
  • 7
  • 22
  • modifying the source code directly is not a good practice, I could do if the library did not give the option to select the backend, but matplolib offers you that ability so I see that your answer is not adequate. – eyllanesc Oct 20 '17 at 13:03
  • For my case, when I used import matplotlib matplotlib.use("Qt5Agg") I got an error – belkacem mekakleb Oct 20 '17 at 14:45
  • The instruction should be placed at the top of your file before any other import – eyllanesc Oct 20 '17 at 14:51