I am using Anaconda with Python 3.5.2, Matplotlib 2.0.2, PyQt5.6 on a windows 10 machine. When I import matplotlib.pyplot as plt I get the following error:
...
File "C:...\Anaconda3\Lib\site-packages\matplotlib\backends\qt_compat.py",
line 137, in <module> from PyQt4 import QtCore, QtGui
ImportError: No module named 'PyQt4'
I don't know why it would want to import from PyQt4 when it has never been installed on my machine.
This question has the same error, but on a machine that actually has PyQt4 installed.
I have checked my matplotlibrc file, I've used matplotlib.use('qt5agg')
in the program, also matplotlib.rcParams['backend'] = "Qt5Agg"
. I've uninstalled and reinstalled all the above packages to no avail. I have even attempted to install PyQt4 just to get things going. I'm completely stumped. None of the various possible causes or remedies that I've been able to find on either SO or github have helped.
I just had the same error (even though on Windows 7). For me the problem was that there was an old matplotlibrc
file in C:\Users\<username>\.matplotlib\matplotlibrc
which overwrote the settings from my environment's matplotlibrc
file. Deleting that file solved the issue for me.
Looking at the file and line number in your error it looks that you have the same issue that I answered here.
That is, I think your issue is caused by having a QT_API
environment variable that still is set to pyqt4
(or pyside
).
I ran into a similar thing and found that the problem was having a 64-bit Anaconda installation that couldn't load the PyPt5 DLLs that were perhaps 32-bit. The short answer is that I uninstalled Anaconda 64-bit and installed the 32-bit version instead.
To work through this, I stepped through qt_compat.py where the error originates. For most of the time through this module, it’s trying to work with PyQt5, as that’s what it finds in the environment. However, when it gets to the lines below, the import fails so it tries to fall back to PyQt4, which isn’t installed with Anaconda, and thus issues the error.
if QT_API == QT_API_PYQT5:
try:
from PyQt5 import QtCore, QtGui, QtWidgets
_getSaveFileName = QtWidgets.QFileDialog.getSaveFileName
except ImportError:
# fell through, tried PyQt5, failed fall back to PyQt4
QT_API = rcParams['backend.qt4']
QT_RC_MAJOR_VERSION = 4
Testing the import statement outside of the file gave the message “DLL load failed: %1 is not a valid Win32 application.” That's what suggests the DLL mismatch, and also answers why it was trying to fall back to PyQt4.
3 steps can solve this problem:
pip uninstall pyqt5
pip uninstall matplotlib
pip install matplotlib
It seems the API version is PyQt5 but the default is PyQt4,just open Tools -> Preferences -> IPython console -> Graphics -> backend,change QT4 to QT5
I had faced the same problem and here is how I fixed it.
Look into your matplotlib configuration file which is often located at
path-to/site-packages/matplotlib/mpl-data/matplotlibrc
or
path-to/Anaconda3/envs/your_env_name/Lib/site-packages/matplotlib/mpl-data/matplotlibrc
See an example of matplotlibrc here
https://github.com/daler/matplotlibrc/blob/master/rc/default
If you have pyqt5.x.x installed and have the statement 'backend : Qt5Agg' in matplotlibrc, then to use '%matplotlib qt', change '#backend.qt4 : PyQt4' to '#backend.qt4 : PyQt5'.
For an Anaconda environment, reactivate the environment.
Note if you update matplotlib in the future, your matplotlibrc will automatically be overwritten.