A Simple Latitude-Longitude Contour Plot

Description

Given a Numeric array of 2-D latitude-longitude data, make a smooth, filled contour plot with color bar.

Sample Data

First we import the packages we'll need:

import Numeric as N
import cdms, vcs

Note that we import Numeric with the shorthand N to save us some typing.

Next let's make some sample data and the longitude and latitude vectors. The grid is global and 15 x 15 degrees. The data is in data and consists of two "distorted bullseyes" centered around each pole (positive over the North Pole and negative over the South Pole). The latitude lat vector goes from 90S to 90N, and the longitude lon vector goes from -180E to 180E:

lat = N.arange(13) * 15.0 - 90.0
lon = N.arange(25) * 15.0 - 180.0
data = N.outerproduct(N.sin(lat*N.pi/360.), N.cos(lon*N.pi/360.))

The data array is 13 x 24, latitude x longitude. This means each row corresponds to a constant latitude, and each column is a constant longitude.

Plotting

To plot the data, the first thing we have to do is initialize a VCS canvas, through instantiating a canvas object:

v = vcs.init()

Then we create CDMS axis objects for the longitude and latitude axis:

lonAxis = cdms.createAxis(lon)
latAxis = cdms.createAxis(lat)

Finally, we use the isofill method of our canvas to plot the data with smooth, filled contours:

v.isofill(data, xaxis=lonAxis, yaxis=latAxis, continents=1, name="Example")

In plotting, VCS automatically assumes the x-axis runs through all the columns of data and the y-axis runs through all the rows. Setting the keyword continents=1 outlines "fine" continents. The keyword name is a string that specifies the title.

The plot looks like:

To write out the plot to a Postscript file eg.ps use:

v.postscript('eg.ps','l')

The 'l' specifies landscape; to print to portrait set the argument to 'p'. Note that plotting portrait will sometimes cut-off the plot at the paper's edge.

To write out the plot to a GIF file eg.gif:

v.gif('eg.gif','r')

The 'r' overwrites eg.gif if it exists.

Although the plotting methods do accept keywords to allow you to control things like axis title in your plot, CDAT's support of these methods can be spotty. To really take advantage of the package, you have to think and use CDAT in terms of objects. This is the approach I take in a more complex latitude-longitude contour plot example.

Full Source Code For Example

Notes: Thanks to Dean Williams for the help! This discussion applies to CDAT 3.3.