Downloading packages

We will be using some packages that are not part of the default Python installation. To check if your Python has them, type the following at the Python shell:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

If there are no errors, then you already have these packages. If not, you will need them. The easiest way is to get the popular packages for scientific computing is to download anaconda distribution of Python. It will install a second copy of Python on your computer (you can still use the old). You can also install matplotlib and numpy separately.

basemap is an extra package for drawing geographic maps. It is not part of many installations and needs to be added. In the anaconda Python, if you type:

from mpl_toolkits.basemap import Basemap
it will give you the exact command to download basemap. You can also download basemap directly:
conda install -c https://conda.anaconda.org/anaconda basemap

The downloads will take about 15-30 minutes, depending on the internet speed. You might want to start the downloads and read ahead while it installs.

basemap in matplotlib

Today, we will use one small part, basemap, of the matplotlib library. It is a very popular for presenting results in 2D plots to be used in papers and presentations. The basemap package of matplotlib allows you to customize maps and then plot them using the standard matplotlib library. Let's first draw some maps, using the build-in projections, and then add points to represent the GIS coordinates of the specimen information from the database.

Drawing Maps

The basemap package follows a familiar format: it stores information in an object and provides functions for manipulating that object. For basemap, the objects are maps (from the Basemap class). The Basemap functions include the ability to change projections, regions, borders, and colors.

To get started, let's draw a simple map of the world. It takes a bit for it to run (you will get a warning telling you this):

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

m = Basemap()
m.drawcoastlines()
plt.show()
To continue, close the map window.

To make the map more interesting, let's add some color. We can do this by using the fillcontinents() function:

m.fillcontinents(color='darkgreen',lake_color='darkblue')
To also fill in the oceans:
m.drawmapboundary(fill_color='darkblue')

(Feel free to alter the colors to make a more attractive map.)

If you would like to use satelite data (NASA 'Blue Marble' imagery), there is a function, bluemarble()

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap()
m.bluemarble()
plt.show()
As well as an option to show the map with shaded relief (shadedrelief()) and etopo relief (etopo()). Try these various `backgrounds' (see map background for more options).

Changing Projections and Regions

There are also options to change the region of the map displayed as well as the projection. The function that constructs the map object has many, many options that control the region projected, the type of projection, and the resolution of coastlines and other features.

For example,

map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
sets up an orthographic map projection with perspective of satellite looking down at 50N, 100W. It uses low resolution coastlines.

Some common projections and useful parameters:

See projections for many, many more.

Some useful things to add to your map:

Plotting Points

We'll first plot a single point, the location of New York City, and then move on to the specimen data.

The coordinates for New York City are: 40.7127 N, 74.0059 W. To use in this package, we use the following conversion:

So, 40.7127 N, 74.0059 W becomes (-74,40.7). To plot it to our map, we first convert it to the map's coordinates, and then plot it:
x,y = m(-74,40)
m.plot(x,y,'ro',markersize=10)
The 'ro' is a matplotlib option to plot red circles and markersize controls how large the plotted point appears.

Challenges