Random Musings for the Digital Age

Compilers, games, and other digital stuff

Installing Matplotlib on OS X 10.7 with Homebrew

[edit: It looks like things have changed a bit since the release of 10.7, so your mileage may vary with this method.  This was written when 10.7 was brand new and most software was not yet updated for it.]

For those of you that do not know, Matplotlib is an excellent Python plotting library that allows you to create professional-quality plots for inclusion on web pages, Latex documents, Beamer presentations, Keynote presentations, and any other software that can import SVG, EPS, PNG, or virtually any graphic format.

However, getting matplotlib installed on Mac OS X 10.7 can be a bit tricky, especially if you are using Homebrew as your “package manager.”  First off, Homebrew does not have packages for matplotlib, as well as some of its dependencies.  Additionally, the current Matplotlib release version (1.0.1 as of this post) does not compile out-of-the-box against libpng 1.5, which is included in the X11 distribution shipped with Mac OS X 10.7.

For previous versions of Mac OS X (10.6, 10.5), the usual way to install matplotlib was to install python, pkg-config, and gfortran with Homebrew, then install numpy and matplotlib through pip, ala:

$ brew install python
$ brew install gfortran
$ brew install pkg-config
$ easy_install pip
$ pip install numpy
$ pip install matplotlib

Unfortunately, as previously mentioned, all is not so easy in the world of Mac OS X 10.7, and the difficulty lies with libpng 1.5, installed with Mac OS X 10.7’s version of X11. Briefly put, Matplotlib 1.0.1 is not compatible with libpng 1.5 due to a change in the API. Fortunately, the fix is already applied up-stream and will probably be a part of Matplotlib 1.0.2, or 1.1.0, or whatever the next released version is.

Until the next release, the Matplotlib sources in Git can be used. Instead of pulling the sources from the Matplotlib SourceForge site, you need to pull them from the Matplotlib GitHub site. I’m not sure if this GitHub site is “official,” but is looks to be.

All that is needed is to build Matplotlib from source instead of using pip, so the installation procedure is now:

$ brew install python
$ brew install gfortran
$ brew install pkg-config
$ easy_install pip
$ pip install numpy
$ cd $HOME
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py build
$ python setup.py install

And now you’re good to go! Hopefully this will become much easier with the next official release of Matplotlib.


18 responses to “Installing Matplotlib on OS X 10.7 with Homebrew

  1. Neil July 22, 2011 at 10:42

    Thanks! This solved my problem.

  2. jd August 3, 2011 at 19:05

    Great… This worked for me… Excellent!!!!
    Although I am on OSX10.6.8, and recently installed python2.7 using Homebrew. But it was BIG problem in installing matplotlib using- $pip install matplotlib (even with option -f ). But this set of instructions worked well! I also tested it, hopefully its fine.
    But I still have a question- the make.osx file still has PYVERSION=2.6 (?)

  3. Evan Davey August 17, 2011 at 05:35

    Worked for me but why gfortran?

    • Justin Holewinski August 17, 2011 at 06:57

      gfortran is a dependency of some parts of numpy. It may be possible to disable the Fortran bindings during the numpy build, but I have not looked too deeply into it.

      • dynetrekk October 9, 2011 at 14:17

        AFAIK, this is simply not true. Numpy _can_ be built against BLAS/LAPACK, for which you need a fortran compiler (e.g. gfortran). However, OS X ships with both a BLAS and a LAPACK implementation pre-compiled, so there is no need to supply these on your own. Also, if no BLAS/LAPACK or fortran compiler is available, you can still build numpy with its own “lapack lite”, which only requires a C compiler.

  4. Pingback: How To Install Python’s Matplotlib on Mac OS X Lion ⊆ David Xia

  5. Peter Beerli August 27, 2011 at 16:24

    I followed your instructions and all went OK, except that once I want to use it I fail the following (1),
    [checking the web some other people hat also problems with this but they had the libfreefonts…. included in the ft2font.so (see further below [2])
    Given the error it misses some font stuff [this is macosx 10.7 and an matplotlib 0.99 works,
    but I need a command from the newer distribution.
    Any idea how to approach this [using brew]

    ImportError: dlopen(/Users/beerli/src/matplotlib/lib/matplotlib/ft2font.so, 2): Symbol not found: _FT_Attach_File
    Referenced from: /Users/beerli/src/matplotlib/lib/matplotlib/ft2font.so
    Expected in: dynamic lookup

    ciguri:histograms>otool -L /Users/beerli/src/matplotlib/lib/matplotlib/ft2font.so
    /usr/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)
    /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 53.0.0)
    /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 41.0.0)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1105.0.0)

    • Justin Holewinski August 29, 2011 at 11:31

      How exactly are you installed matplotlib? Are you installing against the system python 2.6, or the brew python 2.7? In my installation, the ft2font.so file resides in /usr/local/lib/python2.7/site-packages/matploblib. It also has /usr/X11/lib/libfreetype.6.dylib as a dependency, which is what contains the missing symbol. Try installing matplotlib and see if that helps to resolve this link error.

      • Peter Beerli September 8, 2011 at 15:32

        thanks, you are correct!
        I read some more after I posted my message, and found another thread that suggest that one needs

        export CFLAGS=”-I/usr/X11/include -I/usr/X11/include/freetype2 -I/usr/X11/include/libpng12″
        export LDFLAGS=”-L/usr/X11/lib”

        and after that I was able to
        run the the setup.py without a problem

        thanks again for running this site

  6. Subimal Deb September 12, 2011 at 06:18

    Thanks a lot. I was having a compilation problem on Fedora – probably a python 2.7 bug as mentioned at
    Pulling from git solved it for me.

  7. RL October 5, 2011 at 13:57

    Thanks for the help, it works 🙂

  8. ms4py October 13, 2011 at 14:31

    With `pip` you can directly install from a git repo:
    pip install -e git://github.com/matplotlib/matplotlib.git@v1.1.0-rc1#egg=matplotlib

    See pip documentation for details at http://www.pip-installer.org

  9. Steve B. October 21, 2011 at 19:50

    I have tried every single combination of instructions I’ve seen anywhere to install matplotlib on 10.6.8 on my MBP. I’ve gotten it to install on my iMac at work using the instructions above but when I go to actually load matplotlib, I get the following error:
    Traceback (most recent call last):
    File “plot.py”, line 1, in
    import matplotlib.pylab as plt
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/pylab.py”, line 221, in
    from matplotlib import mpl # pulls in most modules
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/mpl.py”, line 2, in
    from matplotlib import axis
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/axis.py”, line 10, in
    import matplotlib.font_manager as font_manager
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/font_manager.py”, line 1323, in
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/font_manager.py”, line 1273, in _rebuild
    fontManager = FontManager()
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/font_manager.py”, line 997, in __init__
    self.afmlist = createFontList(self.afmfiles, fontext=’afm’)
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/font_manager.py”, line 565, in createFontList
    prop = afmFontProperty(fpath, font)
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/font_manager.py”, line 501, in afmFontProperty
    weight = weight_as_number(font.get_weight().lower())
    File “/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.4-x86_64.egg/matplotlib/afm.py”, line 464, in get_weight
    return self._header[‘Weight’]
    KeyError: ‘Weight’

    Does anyone have any ideas?

    • Steve B. October 22, 2011 at 16:03

      Turns out this was neither a problem with python or matplotlib. As someone suggested on the matplotlib users mailing list, the problem in afm.py pointed to bad *.afm files. After inserting a print statement before 565 in afm.py, I was able to identify and isolate 5 font families that were causing the problem.

  10. Blake Johnson December 1, 2011 at 18:19

    In addition to the suggested procedure in the post, I additionally had to install dateutil with:
    pip install python-dateutil==1.5

  11. writeonly December 29, 2011 at 14:02

    For this this worked just fine (which is sugar for the clone / build / setup cycle) on Lion, using “brew python“ python (in a virtualenv):

    pip install -e git://github.com/matplotlib/matplotlib.git#egg=Package

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: