Skip navigation
NASA Logo, National Aeronautics and Space Administration

Version Comparison: Installing MatplotLib and BaseMaps on Mac OSX

Comparing: Revision 10 » Revision 12


 
 
Color Key: Addition Deletion Change
Revision 10   By Shawn Freeman at 8 years, 5 months ago Revision 12   By Shawn Freeman at 6 years, 3 months ago

I. What is MatplotLib and BaseMaps

 

Matplotlib and BaseMaps are python libraries used to display and manipulate data. MatplotLib is the basic package which allows the programmer to create graphs and plots in 2D and 3D (includining animation). The BaseMaps extension adds the additional capabilities to display data overlayed onto a 2D map or 3D globe. The combination of the two allows anyone with familiarity with python a quick and easy way to create analysis tools oor even just some quick visualizations.

 

II. Easiest Install

The easiest way to install both MatPlotLib and BaseMaps is to use DarwinPorts (http://darwinports.com/) or Fink(http://www.finkproject.org/).These applications are "package managers" that will allow you to be able to download all sorts of free software packages for the Mac. You can install either or both applications (the available packages overlap quite a bit).


For Fink, you can install matplotlib and basemaps by running: fink install matplotlib


For DarwinPorts you can install by using this command: port install py-matplotlib-basemap


In either case, this should install everything needed in order to use the python libraries. To test it, use the example here in this document.


This is by far the simplest method of installation. Manual installation isn't difficult but certainly takes a little longer.

 

III. Installing MatplotLib

 

If you want to do this a little more hands on, it's not much more difficult. First, you'll need to download four dmg files:


Python 2.6.5
http://www.python.org/ftp/python/2.6.5/python-2.6.5-macosx10.3-2010-03-24.dmg

Numpy (must be version 1.1 or later)

http://sourceforge.net/projects/numpy/files/NumPy/1.3.0/numpy-1.3.0-py2.6-macosx 10.5.dmg/download

Scipy

http://sourceforge.net/projects/scipy/files/scipy/0.7.1/scipy-0.7.1-py2.6-python .org.dmg/download

And finally, matplotlib

http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-0.99.1/ma tplotlib-0.99.1.1-py2.6-macosx10.5.dmg/download

Install python first. Then install numpy and scipy. Lastly, install matplotlib. These are all dmg’s so they should automatically install everything and set up any environment variables needed.


Once everything is installed, create a new python script, cut and paste one of their examples into it, then try to run it. Here is the one I used:


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
     xs = np.arange(20)
     ys = np.random.rand(20)
     ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()


This should display a 3D bar graph using transparency and multiple colors.


matplotlib.png


Of those reporting problems, it turns out that their .bash_profile (or .cshrc) file was not being called. During install, Python sets up it's path setting there. The .cshrc setting looks like this:

set path=(/Library/Frameworks/Python.framework/Versions/2.6/bin $path)

 

If matplotlib does not run after installing, open a terminal and check the version of python by typing python -V. If the version is 2.5, then your .bash_profile may not be getting called OR you are somehow altering the PATH environment variable to overwrite the python settings.


If that is the case, you'll need to add the python path to your PATH environment variable. The python path should be FIRST in your PATH variable. After that is set, source your file or open a new terminal. The python version should now be 2.6.5. Matplotlib should work after that.

 

IV. Installing BaseMaps From Source

 

This gets a little trickier. There is no pre-built dmg file for installation, so this has to be downloaded and built.


First, get the source from here: http://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/basemap-0.9 9.4/basemap-0.99.4.tar.gz/download


The size is about 100MB, but almost all of that is taken up by map data. Extract the source, then open a temrinal and navigate to the directory.


Before BaseMaps can be built, the GEOS library (nothing to do with the GEOS model) needs to be built. To do so, navigate to the GEOS directory and run configure like so: ./configure --prefix=some/path. After that, you run make and make install. This will create the GEOS libraries.


When the libraries are created, you will need to create a GEOS_DIR environment variable. The GEOS_DIR environemnt variable should match whatever path you used for the prefix when you ran configure. After the environment variable has been created, do a source or whatever action is required to make sure the variable is there.


Now you can go back to the root BaseMap directory and install BaseMaps. To do so, use the command: python setup.py install. This will finish the build process and install the necessary files and libraries to use BaseMaps. To test the installation, you can navigate to the exampless directory and run a few of them.

 

V. Installing BaseMaps From Binary Distribution

 

The setup.py script in BaseMaps gives an option to create a binary distribution. While this is not a self-installing dmg file, it creates a tarball with all the correct folder heirarchy and files for distribution. If you have a binary distribution, installation is easy. Extract the archive and then use cp -R to copy the folder to the root directory.

 

VI. Installing Python Image Library (PIL)

 

This is an optional package but may be necessary depending on whether or not you use certain image sources (see the bluemarble example in BaseMaps).


Again, no binary distribution is available for the Mac so you'll need to build from source. Worse, the actual build fails on the Mac (but it can be worked around).


You can get the source here: http://effbot.org/downloads/Imaging-1.1.7.tar.gz


Extract the source. In a terminal window, navigate to the image library directory. Run python setup.py install. This will most likely fail. The failure arises from the script, which tries to build libraries for both PPC and Intel based machines. Unless you've explicitly installed PPC versions of the required libraries, the python script will fail.


The workaround is simple. There is only one file that this fails on. Copy the failed buid lines beginning with gcc, then paste them on the commandline. After that, go back and delete the -arch ppc part off the command. Once the file builds, re-run the the setup.py script. This should successfully build and instal the library.


To ensure that it installed correctly, you can go to the BaseMaps example directory and try running the bluemarble or warpimage examples.


I. What is MatplotLib and BaseMaps

 

Matplotlib and BaseMaps are python libraries used to display and manipulate data. MatplotLib is the basic package which allows the programmer to create graphs and plots in 2D and 3D (includining animation). The BaseMaps extension adds the additional capabilities to display data overlayed onto a 2D map or 3D globe. The combination of the two allows anyone with familiarity with python a quick and easy way to create analysis tools oor even just some quick visualizations.

 

II. Easiest Install

The easiest way to install both MatPlotLib and BaseMaps is to use DarwinPorts (http://darwinports.com/) or Fink(http://www.finkproject.org/).These applications are "package managers" that will allow you to be able to download all sorts of free software packages for the Mac. You can install either or both applications (the available packages overlap quite a bit).


For Fink, you can install matplotlib and basemaps by running:

 

 

fink install matplotlib

 


For DarwinPorts you can install by using this command:

 

 

port
install
install  py-matplotlib-basemap

 


In either case, this should install everything needed in order to use the python libraries. To test it, use the example here presented below in this document.


This is by far the simplest method of installation. Manual installation isn't difficult but certainly takes a little longer.

 

III. Installing MatplotLib

 

If you want to do this a little more hands on, it's not much more difficult. First, you'll need to download four dmg files:


  1. Python 2.
6
  1. 7.
5
  1. 3:
6
5
6
5
-2010-03-24
  1. Numpy
(must be version 1.1 or later)
  1. :
3
0
3
0
6
5

Scipy

  1. SciPy:
7
1
7
1
6
python

And finally, matplotlib

  1. Matplotlib:
0
99
ma tplotlib
0
99.
6
5


Install python Python first. Then install numpy Numpy and scipySciPy. Lastly, install matplotlibMatplotlib. These are all dmg’s so they should automatically install everything and set up any environment variables needed.. The installation will be done in the directory:

 

/Library/Frameworks/Python.framework/Versions/2.7

 

You will then need to set up your environment variables (.cshrc file for instance):

 

set path=(/Library/Frameworks/Python.framework/Versions/2.6/bin $path)

 

Once everything is installed, create a new python script, cut and paste one of their examples into it, then try to run it. Here is the one I used:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
     xs = np.arange(20)
     ys = np.random.rand(20)
     ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()


This should display a 3D bar graph using transparency and multiple colors.


matplotlib.png

Of those reporting problems, it turns out that their .bash_profile (or .cshrc) file was not being called. During install, Python sets up it's path setting there. The .cshrc setting looks like this:

set path=(/Library/Frameworks/Python.framework/Versions/2.6/bin $path)


 

If matplotlib does not run after installing, open a terminal and check the version of python by typing python -V. If the version is 2.5, then your .bash_profile may not be getting called OR you are somehow altering the PATH environment variable to overwrite the python settings.

If that is the case, you'll need to add the python path to your PATH environment variable. The python path should be FIRST in your PATH variable. After that is set, source your file or open a new terminal. The python version should now be 2.6.5. Matplotlib should work after that.

 

IV. Installing BaseMaps From Source

 

This gets a little trickier. There is no pre-built dmg file for installation, so this has to be downloaded and built.


First, get the source from here: http://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/basemap-0.9 9.4/basemap-0.99.4.tar.gz/download


The size is about 100MB, but almost all of that is taken up by map data. Extract the source, then open a temrinal and navigate to the directory.


Before BaseMaps can be built, the GEOS library (nothing to do with the GEOS model) needs to be built. To do so, navigate to the GEOS directory and run configure like so: ./configure --prefix=some/path. After that, you run make and make install. This will create the GEOS libraries.


When the libraries are created, you will need to create a GEOS_DIR environment variable. The GEOS_DIR environemnt variable should match whatever path you used for the prefix when you ran configure. After the environment variable has been created, do a source or whatever action is required to make sure the variable is there.


Now you can go back to the root BaseMap directory and install BaseMaps. To do so, use the command: python setup.py install. This will finish the build process and install the necessary files and libraries to use BaseMaps. To test the installation, you can navigate to the exampless directory and run a few of them.

 

V. Installing BaseMaps From Binary Distribution

 

The setup.py script in BaseMaps gives an option to create a binary distribution. While this is not a self-installing dmg file, it creates a tarball with all the correct folder heirarchy and files for distribution. If you have a binary distribution, installation is easy. Extract the archive and then use cp -R to copy the folder to the root directory.

 

VI. Installing Python Image Library (PIL)

 

This is an optional package but may be necessary depending on whether or not you use certain image sources (see the bluemarble example in BaseMaps).


Again, no binary distribution is available for the Mac so you'll need to build from source. Worse, the actual build fails on the Mac (but it can be worked around).


You can get the source here: http://effbot.org/downloads/Imaging-1.1.7.tar.gz


Extract the source. In a terminal window, navigate to the image library directory. Run python setup.py install. This will most likely fail. The failure arises from the script, which tries to build libraries for both PPC and Intel based machines. Unless you've explicitly installed PPC versions of the required libraries, the python script will fail.


The workaround is simple. There is only one file that this fails on. Copy the failed buid lines beginning with gcc, then paste them on the commandline. After that, go back and delete the -arch ppc part off the command. Once the file builds, re-run the the setup.py script. This should successfully build and instal the library.


To ensure that it installed correctly, you can go to the BaseMaps example directory and try running the bluemarble or warpimage examples.


USAGov logo NASA Logo - nasa.gov