Line Plots in VCS

Return to Python Lectures

The basics

VCS makes it very easy to get a basic contour map of 2D data, but getting a standard x vs. y graph using the plot( ) method is a bit tricky and unintuitive. The default form of plot for the plot( ) method is a box-fill map, and if you want something else, you need to specify additional parameters. The 'plot method' is a positional parameter, which must be put in the right place in the argument list of plot. The following example shows a plot of sin(x) vs x, using data we create locally:

import math,Numeric,cdms,vcs
#Create the data arrays. This could be done in a loop, but we
#use "list comprehension" instead to create the arrays in a non-loopy way
#The following statement shows how Numeric.array creates a Numeric array
#from a Python list
xlist = [math.pi*i/99. for i in range(100)]
X = Numeric.array(xlist)
Y = Numeric.array([math.sin(x) for x in xlist])
# The Y array could also have been created using Numeric.fromfunction( )
#Now open a window and plot the data
w = vcs.init()

The first two arguments to plot( ) are the 1D X and Y arrays that are to be plotted. The next argument is the "template" which specifies what the plot should look like. We need to include it even though we want the default template, because the plot type is a positional argument that has to come fourth in the list. The 'xvsy' is the plot method, that specifies that we want an x-y graph, rather than the default contour plot.

VCS provides a number of alternatives ways to specify the kind of plot wanted. For example, instead of using the generic "plot" method, one could use the special named method for the type of plot desired.

<insert example of xvsy plot method, mention isofill, etc.>

<How to get multiple curves on one graph>

Line plots from cdms variables

Often, one wants to graph data in a cdms Variable that one has read in from a file. For example, let's read in some temperature data on a time-lat-lon grid from a file which has the time, latitude and longitude axes properly defined so cdms can recognize them as axes (rather than just another bit of data). We also get the latitude axis, for use as the x-axis in the plot we want to make.

import cdms,vcs
f ='')
T = f['TS']
lat = f['lat']

You would think that you could just use lat as the X scale in a plot command, but you can't. The reason is that, while lat looks a bit like an array, it's actually a cdms axis structure, which has a lot more to it than an ordinary array. (to find out if something is an axis, just type it's name. If it's an axis, you'll get back a lot of information on its properties). It would make a lot of sense if plot( ) could just take an axis structure as an argument, but it can't (as of present writing). The workaround is very simple: just refer to the axis data as an array cross section, as follows:

w = vcs.init()

We needed to refer to T as an array cross section anyway, since it's a 2D array and we need 1D data for a line plot. The above example plots T vs. latitude, for time index 0 and longitude index 12. If your data is higher dimensional, you can extract whatever cross section you want, as long as it comes out 1D and it has length equal to the length of whatever you specified for the x axis.

The following example shows how to get a time series plot from the same data:

t = f['time']

Return to Python Lectures