Update: This tutorial has been updated and moved to. Python’s package is an amazing resource, and the 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.
The lxml library is a dependency for many popular Python projects such as Scrapy and Nikola. If you are a Mac OSX user, it's highly likely that you have encountered the following error when trying to install lxml or its dependants with pip: In file included from src/lxml/lxml.
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. 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. 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. 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, 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 GEOSDIR 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 GEOSDIR to /usr/local/: basemap-1.x.x $ cd geos-3.3.3 basemap-1.x.x/geos-3.3.3 $ export GEOSDIR=/ basemap-1.x.x/geos-3.3.3 $./configure -prefix=$GEOSDIR 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.
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. Easiest Install The easiest way to install both MatPlotLib and BaseMaps is to use DarwinPorts ( ) or Fink( 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 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. 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.7. 3:. Numpy:. SciPy:. Matplotlib:. Install Python first.
Then install Numpy and SciPy. Lastly, install Matplotlib. These are all dmg’s so they should automatically install everything.
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 mpltoolkits.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.setxlabel('X') ax.setylabel('Y') ax.setzlabel('Z') plt.show This should display a 3D bar graph using transparency and multiple colors.
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: 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 GEOSDIR environment variable. The GEOSDIR 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.
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. 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: 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.