Pages

Monday, May 14, 2018

Fixing the Matplotlib PyPlot import errors

About a week back, I was reinstalling Keras, TensorFlow and all the other libraries after a reformat of my PC. When I started verifying the library installations, I came across a strange error. When I tried to run a simple deep learning model, Python runtime crashed. As soon as I execute the script I was getting the "python.exe has stopped working" error message (I'm using Windows 10).

Matplotlib is an important part of my deep learning workflows
Matplotlib is an important part of my deep learning workflows
A little bit of debugging narrowed down the error to the following line in my script.

import matplotlib.pyplot as plt

(I was using matplotlib to graph the training history of the model. See "How to Graph Model Training History in Keras")

The error did not occur if I simply import matplotlib. It only occurred when specifically importing the pyplot module.

import matplotlib
# no errors

import matplotlib.pyplot as plt
# crash!!!

This is a known issue due to some library conflicts in the installation, which should hopefully be fixed in a future release. Until then, if you're getting this error, you can fix it by following the steps below.



Try them in order and see which step works for you.

1. Check if multiple versions of numpy is installed, and remove any unnecessary versions if possible.

2. Try updating matplotlib

conda update matplotlib

3. Force reinstall matplotlib using conda

conda install -f matplotlib

4. Clean out any version of matplotlib which might have been installed using pip

conda remove matplotlib
pip uninstall matplotlib
conda install matplotlib




5. If all other steps fail, try removing the conda version of matplotlib, and installing using pip instead

conda remove matplotlib
pip install matplotlib

Hopefully, these steps would help you get your matplotlib import working correctly again.

Update: As of January 2020, the error has been fixed in the latest version of Matplotlib (3.1.1).

Related links,

Build Deeper: The Path to Deep Learning

Learn the bleeding edge of AI in the most practical way: By getting hands-on with Python, TensorFlow, Keras, and OpenCV. Go a little deeper...

Get your copy now!

2 comments:

  1. matplotlib-3.2.1 still not working for "import matplotlib.pyplot as plt" in python script. Windows7, 64-bit OS. Command line installation of Python38 and pip install of numpy, scipy, matplotlib, and only matplotlib causes an error. So, to me, it seems matplotlib continues to be a problem for many "novices" while other packages appear to work correctly. Seems to me matplotlib isn't as well written as the other packages.
    When including line import matplotlib as mpl, and having no other active lines of code, I get no error message. As soon as I un-comment the line, import pyplot as plt, or, alternatively use the line import matplotlib.pyplot as plt instead of import matplotlib as mpl, I get the non-existent package error. So far, nobody's "fix" has fixed the issue.

    ReplyDelete
  2. Thanks for your guidance...
    its work for me...

    ReplyDelete