Python 2.7: Unable to import matplotlib.pyplot

31,198

Solution 1

as presented here: https://askubuntu.com/questions/636937/python-2-7-matplotlib-provides-errorpyplot

this command will fix it: conda install matplotlib

and indeed it helped me.

Solution 2

I use Kali Linux,I use: pip install matplotlib

Share:
31,198
zzhengnan
Author by

zzhengnan

Updated on January 01, 2020

Comments

  • zzhengnan
    zzhengnan over 4 years

    When I tried to

    import matplotlib.pyplot as plt
    

    I was given the following error:

    runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')
    Traceback (most recent call last):
    
    File "<ipython-input-2-9001323bba15>", line 1, in <module>
    runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)
    
    File "/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py", line 4, in <module>
    import matplotlib.pyplot as plt
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
    import matplotlib.colorbar
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
    import matplotlib.collections as collections
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
    import matplotlib.backend_bases as backend_bases
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
    import matplotlib.textpath as textpath
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in <module>
    from matplotlib.mathtext import MathTextParser
    
    File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in <module>
    import matplotlib._png as _png
    
    ImportError: dlopen(/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: @loader_path/../../../libpng15.15.dylib
    Referenced from: /Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
    Reason: image not found
    

    Can anyone please help me? I'm a newbie and not really familiar with paths. A step-by-step instruction would be greatly appreciated. Thanks in advance.

    BTW, I'm using spyder as an IDE.

    • Admin
      Admin about 9 years
      Look at the (last lines of) the error: you don't have libpng installed. Install that library first, then (re)install matplotlib.
    • Admin
      Admin about 9 years
      How did you install matplotlib (ah, probably conda)? And what package manager (if any) are you using on your machine (fink, macports, homebrew)?
    • Admin
      Admin about 9 years
      @user3636636 It is likely not the same problem: the current question appears to be a anaconda installation problem (not a standard pip installation), and anaconda appears to ship it's own library. Also: the error messages are (quite) different.
    • tacaswell
      tacaswell about 9 years
      run a conda update --all, The version of libpng they shipped got bumped recently and if you updated things in a funny (by doing an install which updated libpng, but mpl didn't get flag as needing an update) you can have an inconsistent installation.
  • zzhengnan
    zzhengnan about 9 years
    Thanks for the answer. I only have libpng16.16.dylib in the directory you suggested. I installed anaconda, and all the packages came with it pre-installed.
  • Admin
    Admin about 9 years
    @Nero You mean you had installed anaconda (before you asked here on SO), and the default libpng anaconda brings along is version 16, but their matplotlib links against version 15? If so, I'd bring that up with the anaconda people. If your problem is not resolved yet (I can't really tell from your comment), I can update my answer with a suggestion to fix this for now; just let me know.