NCL plot creation sequence

This module describes a basic structure for an NCL script used to create and display graphics. Complicated tasks may require a more elaborate structure. After experience with using NCL, you may find a structure that is more suitable to your needs and preferences, but the following will get you started. A very simple example is provided below. For more sophisticated examples, consult the Quick Start Guide.

Basic structure:

  1. Create or import any relevant data
  2. Create output devices for graphic display
  3. Create plot objects
  4. Draw plot objects on output devices
  5. Modify plot attributes and redraw

Create or import any relevant data

There are several ways that you may create the data needed for your plots in NCL. You may generate the data internally within NCL and store it in an NCL variable--this is illustrated in the simple example below. Also, you may import data from a previously-existing file. Various formats are supported for the imported file. For details on importing and exporting data in NCL, see Importing and exporting data.

After your datasets have been created or imported, you may want to manipulate the data. For example, you may want to convert temperatures from Fahrenheit to Celsius. Or you may want to compute quantities based on the imported data, thereby creating new data fields. NCL has a wide selection of internal functions and procedures to help you with data manipulation. See the module on Data manipulation for details.

Create output devices for graphic display

You need to create workstation objects that specify the output devices on which you want to display your graphics. You can display your view objects in X11 windows, or produce an NCAR CGM file, or produce PostScript output. For more details on workstation objects, see the HLU module on the Workstation class. Also see the module on Managing your output for a discussion of selecting various output devices.

Create plot objects

NCL offers a powerful but simple interface for creating plot objects and for setting and modifying the resource values of such objects. For a discussion of what objects are and how they are used, see the appropriate topics in the High Level Utilities section of the User Guide. For details on creating plot objects in NCL, see the module Visualization specification process.

Draw plot objects on output devices

Once your view objects have been created, it is a simple matter to plot them on any of the output devices you have created. See the module on Visualizing images for details.

Modify plot attributes and redraw

Once you have plotted a view object on a workstation, you may want to modify the values in its resource file and draw it again on the same workstation, or on a different workstation. You may want to scale it or change its position on the output device. For details on how to do these things, see the Manipulating images module.

Delete objects

Before terminating NCL, it is good form to delete all the objects you have created. The NCL procedures delete and destroy can be used for this.

A simple example

Here is an extremely simple NCL script that illustrates the steps mentioned above.

;
; Simple NCL script just to illustrate the NCL plot generation structure
; discussed in this module.  
;

;
; Draw an X-Y plot with internally-generated data.
;

;
; Step 1 - create linear arrays containing the data to be plotted.
;          This data could have been read in using one of the read
;          functions, or by doing an addfile on a file in a supported
;          data format.
;

x_data = (/ -2., -1., 0., 1., 2., 3. /)
y_data = (/  2.,  1., 0., 1., 2., 3. /)

;
; Step 2 - create an X11 window to use as an output device for displaying
;          the plot. Set the pause option so that a call to "frame" will
;          pause waiting for a mouse click or keyboard entry.
;

xworkid = create "simple" xWorkstationClass defaultapp
    "wkPause" : "True"
end create

;
; Step 3 - create an XyPlot object using the data in x_data and y_data.
;

;
;          First create the data object.
;

dataid = create "xyData" coordArraysClass defaultapp
    "caXArray": x_data
    "caYArray": y_data
end create

;
;          Next, create the XyPlot object which is a child of the 
;          XWorkstation object. Set the appropriate resource
;          to force recomputation of plot limits when the data
;          is changed.
; 

plotid = create "xyPlot" xyPlotClass xworkid
    "xyCoordData": dataid
    "xyComputeYMax": "True"
end create

;
; Step 4 - Draw the plot.
; 

draw(plotid)
frame(xworkid)

;
; Step 5 - Modify the data and redraw.
;

y_data = y_data^2

;
;          Modify the data object.
;
setvalues dataid
    "caYArray": y_data
end setvalues

;
;          Inform the plot object of the data change.
;
setvalues plotid
    "xyCoordData": dataid
end setvalues

;
;          Redraw
;
draw(plotid)
frame(xworkid)


User Guide Control Panel

NG4.1 Home, Index, Examples, Glossary, Feedback, UG Contents, UG WhereAmI?


$Revision: 1.7 $ $Date: 1998/06/15 22:08:57 $