Reinstalling Matplotlib on Mac

legaultmarc picture legaultmarc · Apr 3, 2014 · Viewed 9k times · Source

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.

Answer

Frederic picture Frederic · Apr 4, 2014

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.