Matplotlib Basemap Tutorial: Installing matplotlib and Basemap

Update:

This tutorial has been updated and moved to http://introtopython.org/visualization_earthquakes.html.

Python’s matplotlib package is an amazing resource, and the Basemap toolkit extends matplotlib’s capabilities to mapping applications.  Installing these packages is straightforward using Ubuntu’s package management system, but these packages may not work for all maps you want to produce.  In that case, it is good to know how to install each package from source.

Installing Basemap using Ubuntu’s standard packages

If you are running a newer version of Ubuntu, you may be able to get away with simply installing matplotlib and basemap through the standard repositories.  This approach should work well enough to get you started, but if you notice some odd behavior with your maps you might need to go back and install both packages from source.

sudo apt-get install python-matplotlib
sudo apt-get install python-mpltoolkits.basemap

If these ran successfully, you can move on to Making a simple map.  If not, you might want to try installing matplotlib and basemap from source.

Installing from source

Installing from source takes a bit longer, but it’s a fairly straightforward process if you have a decent internet connection.

First, let’s remove any versions of matplotlib and basemap that have been installed through the repositories:

sudo apt-get remove --purge python-matplotlib
sudo apt-get remove --purge python-mpltoolkits.basemap

Install matplotlib from source

These instructions are adapted from the matplotlib documentation.

We start by installing dependencies.  This is a 200-500MB download, depending on what you already have installed:

sudo apt-get build-dep python-matplotlib

Now install git, if you have not done so already:

sudo apt-get install git

Now we need to clone the git repository for matplotlib.  You can do this in your home folder, or somewhere like /usr/local/ if that is where you normally install packages from source.  Once we have a local matplotlib repository, we need to run the install script:

~$ git clone git://github.com/matplotlib/matplotlib.git
~$ cd matplotlib
~$ python setup.py install

Install Basemap from source

These installation instructions are adapted from the Basemap documentation.

First we need to download the source code.  As of this writing, the newest version of Basemap is 1.0.5.  Look for the newest version here, and click the folder.  On the next page, you  are looking for a file called basemap-1.x.x.tar.gz.  This is the file you want to download. It is about 120MB, so again you will need a decent internet connection.

When the download is complete, open the archive and drag the basemap-1.x.x folder to your home folder, or /usr/local/, or wherever you keep your source code.

We will first install geos.  From the basemap-1.x.x directory, we need to change into the geos directory and set the GEOS_DIR environment variable.  These directions assume you are working from your home directory; if you are working in /usr/local/, you will probably need to change the GEOS_DIR to /usr/local/:

basemap-1.x.x $ cd geos-3.3.3
basemap-1.x.x/geos-3.3.3 $ export GEOS_DIR=~/
basemap-1.x.x/geos-3.3.3 $ ./configure --prefix=$GEOS_DIR
basemap-1.x.x/geos-3.3.3 $ make
basemap-1.x.x/geos-3.3.3 $ make install

Now we need to change back to the basemap parent directory, and install basemap:

basemap-1.x.x/geos-3.3.3 $ cd ..
basemap-1.x.x $ python setup.py install

You can test if everything worked by running an example.  From your basemap-1.x.x directory, change into the examples directory and run simpletest.py.  Basemap is a large library, so the example script may take a while to run:

basemap-1.x.x $ cd examples
basemap-1.x.x/examples $ python simpletest.py

You should see something like this:

An example map produced from the Basemap toolkit.

An example map produced from the Basemap toolkit. If you see this, things have installed correctly.

If you see a map similar to this, things have installed correctly.  You can see some more examples if you like.  There are a fair number of examples, so looking at all of them might take a while.  But they are pretty interesting, so you might enjoy it:

basemap-1.x.x/examples $ python run_all.py

Now that we have basemap installed and working, we can move on to making our own simple map.

Next: Making a simple map

Back to Tutorial contents

Advertisements

About ehmatthes

Teacher, hacker, new dad, outdoor guy
This entry was posted in programming and tagged , , , , . Bookmark the permalink.

4 Responses to Matplotlib Basemap Tutorial: Installing matplotlib and Basemap

  1. Aliton says:

    I guess you forgot:
    basemap-1.x.x/geos-3.3.3 $ ./configure –prefix=$GOES_DIR
    after command:
    basemap-1.x.x/geos-3.3.3 $ export GEOS_DIR=~/

  2. Anonymous says:

    any steps for mac?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s