5. Plotting CDMS data in Python¶
5.1. Overview¶
Data read via the CDMS Python interface can be plotted using the vcs
module. This module, part of the Climate Data
Analysis Tool (CDAT) is documented in the CDAT reference manual.
The vcs
module provides access to the functionality of the VCS
visualization program.
Examples of plotting data accessed from CDMS are given below, as well as documentation for the plot routine keywords.
5.2. Examples¶
In the following examples, it is assumed that variable psl
is
dimensioned (time, latitude, longitude). psl
is contained in the
dataset named 'sample.xml'
.
5.2.1. Plotting a Gridded Variable¶
- Example:
Plotting a gridded variable
1 2 3 4 5 6 7 8 | >>> import cdms2, vcs
>>> f = cdms2.open("clt.nc")
>>> clt = f.variables['clt']
>>> sample = clt[0,:]
>>> w=vcs.init()
>>> w.plot(sample)
<vcs.displayplot.Dp object ...>
>>> f.close()
|
Notes:
Line |
Notes |
---|---|
3 |
Get a horizontal slice, for the first time point. |
4 |
Create a VCS Canvas |
5 |
Plot the data. Because sample is a transient variable, it encapsulates all the time, latitude, longitude, and attribute information. |
7 |
Close the file. This must be done after the reference to the persistent variable |
Thats it! The axis coordinates, variable name, description, units, etc. are obtained from variable sample.
What if the units are not explicitly defined for clt
, or a different
description is desired? plot
has a number of other keywords which
fill in the extra plot information.
5.2.2. Using A Plot Keywords¶
1 2 3 4 5 6 7 8 | >>> import cdms2, vcs
>>> f = cdms2.open("clt.nc")
>>> clt = f.variables['clt']
>>> sample = clt[0,:]
>>> w=vcs.init()
>>> w.plot(sample, units='percent', file_comment='', long_name="Total Cloud", comment1="Example plot", hms="00:00:00", ymd="1979/01/01")
<vcs.displayplot.Dp object ...>
>>> f.close()
|
Note: Keyword arguments can be listed in any order.
5.2.3. Plotting a Time-Latitude Slice¶
Assuming that variable clt
has domain (time,latitude,longitude)
,
this example selects and plots a time-latitude slice:
1 2 3 4 5 6 7 | >>> import cdms2, vcs
>>> f = cdms2.open("clt.nc")
>>> clt = f.variables['clt']
>>> samp = clt[:,:,0]
>>> w = vcs.init()
>>> w.plot(samp, name='Total Cloudiness')
<vcs.displayplot.Dp object ...>
|
Line |
Notes |
---|---|
4 |
|
6 |
The |
5.2.4. Plotting Subsetted Data¶
Calling the variable clt
as a function reads a subset of the
variable. The result variable samp
can be plotted directly:
1 2 3 4 5 6 7 8 | >>> import cdms2, vcs
>>> f = cdms2.open("clt.nc")
>>> clt = f.variables['clt']
>>> samp = clt(time = (0.0,100.0), longitude = 180.0, squeeze=1)
>>> w = vcs.init()
>>> w.plot(samp)
<vcs.displayplot.Dp object ...>
>>> f.close()
|
5.3. Plot Method¶
The plot
method is documented in the CDAT Reference Manual. This
section augments the documentation with a description of the optional
keyword arguments. The general form of the plot command is:
canvas.plot(array [, args] [,key=value [, key=value [, ...] ] ])
where:
canvas is a VCS Canvas object, created with the vcs.init method.
array is a variable, masked array, or Numpy array having between two and five dimensions. The last dimensions of the array is termed the ‘x’ dimension, the next-to-last the ‘y’ dimension, then ‘z’, ‘t’, and ‘w’.
For example, if array is three-dimensional, the axes are (z,y,x), and if array is four-dimensional, the axes are (t,z,y,x).
Note: that the t dimension need have no connection with time; any spatial axis can be mapped to any plot dimension.)
For a graphics method which is two-dimensional, such as boxfill, the y-axis is plotted on the horizontal, and the x-axis on the vertical.
If array is a gridded variable on a rectangular grid, the plot function uses a box-fill graphics method.
If it is non-rectangular, the meshfill graphics method is used.
Note: that some plot keywords apply only to rectangular grids only.
args are optional positional arguments:
args
:= template_name, graphics_method, graphics_nametemplate_name
: the name of the VCS template (e.g., ‘AMIP’)graphics_method
: the VCS graphics method (boxfill)graphics_name
: the name of the specific graphics method (‘default’)See the CDAT Reference Manual and VCS Reference Manual for a detailed description of these arguments.
key=value
, … are optional keyword/value pairs, listed in any order. These are defined in the table below.
5.3.1. Plot Keywords¶
Key |
Type |
Value |
---|---|---|
comment1 |
string |
Comment plotted above |
comment2 |
string |
Comment plotted above |
comment3 |
string |
Comment plotted above |
continents |
0 or 1 |
|
file_comment |
string |
|
grid |
CDMS grid object |
|
hms |
string |
Hour, minute, second |
long_name |
string |
|
missing_value |
same type as array |
|
|
string |
|
time |
cdtime relative or absolute |
|
5.3.2. Plot Keywords(cont’d)¶
Key |
Type |
Value |
---|---|---|
units |
string |
|
variable |
CDMS variable object |
|
xarray ( |
1-D Numpy array |
|
xaxis ( |
CDMS axis object |
|
xbounds ( |
2-D Numpy array |
|
xname ( |
string |
|
xrev ( |
0 or 1 |
If
|
xunits ( |
string |
|