Question

I upgraded packages and broke my matplotlib installation. Now when I run import matplotlib.pyplot as plt, I get the following error:

ImportError: dlopen(/usr/local/lib/python2.7/site-packages/matplotlib/ft2font.so, 2):       
Library not loaded: /usr/local/lib/libpng16.16.dylib
  Referenced from: /usr/local/lib/libfreetype.6.dylib
  Reason: image not found

To fix this, I uninstalled freetype and libpng using brew. Specifically, I used brew uninstall freetype && brew cleanup && brew install freetype and the same commands with libpng. The I reinstalled matplotlib (pip uninstall matplotlib, then pip install matplotlib).

This did not fix it, and I don't know what the next step is to be able to have a working matplotlib (.pyplot) installation.

Was it helpful?

Solution

I had a very similar issue running OS X 10.9.2 (Mavericks) and trying to get IPython 2.0 working. Here is what I did to fix it.

Step #1: Uninstall matplotlib:

$ pip uninstall matplotlib

Step #2: Use libpng v1.5.17:

$ brew update
$ cd `brew --prefix` (this brought me to /usr/local)
$ brew versions libpng
$ git checkout c22afb9 Library/Formula/libpng.rb (this gets version 1.5.17)
$ brew install libpng
$ brew switch libpng 1.5.17
$ git checkout -- Library/Formula/libpng.rb

Step #3: Use freetype v2.5.0.1:

$ cd `brew --prefix` (this brought me to /usr/local)
$ brew versions freetype
$ git checkout 6314fdb Library/Formula/freetype.rb (this gets version 2.5.0.1)
$ brew install freetype
$ brew switch freetype 2.5.0.1
$ git checkout -- Library/Formula/freetype.rb

Step #4: Re-install matplotlib:

$ CFLAGS="-I/usr/X11/include -I/usr/X11/include/freetype2" pip install matplotlib

NOTE: You may need to adjust your CFLAGS to match where you have the freetype headers installed (but I'm assuming if you had matplotlib installed earlier, you know how to re-install it again)


Now I have a working matplotlib:

$ python
Python 2.7.6 (default, Mar  3 2014, 15:04:57) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> 

It has fixed the matplotlib issue I was having in IPython.

I hope this works for you too.

OTHER TIPS

I have met the same problem. Thanks to galvanist's blog, the following solution works well for me. The sudo before pip can be removed if you have the permisson (for example, using the virtualenv).

# possibly optional for you
sudo pip uninstall matplotlib

# possibly optional for you
sudo rm -rf /tmp/pip-build-root/

brew reinstall libpng --universal

brew reinstall freetype --universal

sudo pip install matplotlib

This is a recommendation.

I had the same problem like you. I had installed Anaconda on OSX in advanced. But my matplotlib seems corrupted anyway. So I remove matplotlib and reinstall it with pip. The the problem appeared.

In my case, Frederic's solution can resolve image not found problem. But another problem shows up:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/pyplot.py", line 97, in <module>
  _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/__init__.py", line 25, in pylab_setup 
    globals(),locals(),[backend_name])   
  File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/backend_macosx.py", line 21, in <module>
    from matplotlib.backends import _macosx
  ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/_macosx.so, 2): Library not loaded: /System/Library/Frameworks/CoreText.framework/Versions/A/CoreText
    Referenced from: /Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/_macosx.so

My final solution is reinstall matplotlib with conda install matplotlib. Then all problems resolved.

for those who got the brew versions error, it is an obsolete command. I got it working by going straight to to brew install libpng and brew install freetype.

Install brew: $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ cd `brew --prefix` (this brought me to /usr/local)
$ brew install libpng
$ brew install freetype
$ easy_install matplotlib

After I did that, I relaunched Spyder and the matploblib error went away

for those met the same issue on Spyder. You might use conda install libpng to address issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top