Direct Graphics Coordinate Systems

You can specify coordinates to IDL in one of the following coordinate systems:

DATA Coordinates

This coordinate system is established by the most recent PLOT, CONTOUR, or SURFACE procedure. This system usually spans the plot window, the area bounded by the plot axes, with a range identical to the range of the plotted data. The system can have two or three dimensions and can be linear, logarithmic, or semi-logarithmic. The mechanisms of converting from one coordinate system to another are described below. See CONVERT_COORD Function.

DEVICE Coordinates

This coordinate system is the physical coordinate system of the selected plotting device. Device coordinates are integers, ranging from (0, 0) at the bottom-left corner to (Vx -1, Vy -1) at the upper-right corner. Vx and Vy are the number of columns and rows addressed by the device. These numbers are stored in the system variable !D as !D.X_SIZE and !D.Y_SIZE.

NORMAL Coordinates

The normalized coordinate system ranges from zero (0) to one (1) over each of the three axes.

Almost all of the IDL graphics procedures accept parameters in any of these coordinate systems. Most procedures use the data coordinate system by default. Routines beginning with the letters TV are notable exceptions. They use device coordinates by default. You can explicitly specify the coordinate system to be used by including one of the keyword parameters /DATA, /DEVICE, or /NORMAL in the call.

Two-Dimensional Coordinate Conversion

The system variables !D, !P, !X, !Y, and !Z contain the information necessary to convert from one coordinate system to another. The relevant fields of these system variables are explained below, and formulae are given for conversions to and from each coordinate system. See Plotting Multi-Dimensional Arrays for a discussion of three-dimensional coordinates.

In the following discussion, D is a data coordinate, N is a normalized coordinate, and R is a raw device coordinate.

The fields !D.X_VSIZE and !D.Y_VSIZE always contain the size of the visible area of the currently selected display or drawing surface. Let Vx and Vy represent these two sizes.

The field !X.S is a two-element array that contains the parameters of the linear equation, converting data coordinates to normalized coordinates. !X.S[0] is the intercept, and !X.S[1] is the slope. !X.TYPE is 0 for a linear x-axis and 1 for a logarithmic x-axis. The y- and z-axes are handled in the same manner, using the system variables !Y and !Z.

Also, let Dx be the data coordinate, Nx the normalized coordinate, Rx the device coordinate, Vx the device X size (in device coordinates), and Xi = !X.S style="font-style: italic" class="cSubScript">i (the scaling parameter).

With the above variables defined, the linear two-dimensional coordinate conversions for the x coordinate can be written as follows:

Coordinate Conversion
Linear
Logarithmic
Data to normal
Data to device
Normal to device
Normal to data
Device to data
Device to normal

The y- and z-axis coordinates are converted in exactly the same manner, with the exception that there is no z device coordinate and that logarithmic z-axes are not permitted.

CONVERT_COORD Function

The CONVERT_COORD function provides a convenient means of computing the above transformations. It can convert coordinates to and from any of the above systems. The keywords DATA, DEVICE, or NORMAL specify the input system. The output coordinate system is specified by one of the keywords TO_DATA, TO_DEVICE, or TO_NORMAL. For example, to convert the endpoints of a line from data coordinates (0, 1) to (5, 7) to device coordinates, use the following statement:

```D = CONVERT_COORD([0, 5], [1, 7], /DATA, /TO_DEVICE)
```

On completion, the variable D is a (3, 2) vector, containing the x, y, and z coordinates of the two endpoints.

X Versus Y Plots-PLOT and OPLOT

This section illustrates the use of the basic x versus y plotting routines, PLOT and OPLOT. PLOT produces linear-linear plots by default, and can produce linear-log, log-linear, or log-log plots with the addition of the XLOG and YLOG keywords.

Data used in these examples are from a fictitious study of Pacific Northwest Salmon fisheries. In the example, we suppose that data were collected in the years 1967, 1970, and from 1975 to 1983. The following IDL statements create and initialize the variables SOCKEYE, COHO, CHINOOK, and HUMPBACK, which contain fictitious fish population counts, in thousands, for the 11 observations:

```SOCKEYE=[463, 459, 437, 433, 431, 433, 431, 428, 430, 431, 430]
COHO=[468, 461, 431, 430, 427, 425, 423, 420, 418, 421, 420]
CHINOOK=[514, 509, 495, 497, 497, 494, 493, 491, 492, 493, 493]
HUMPBACK=[467, 465, 449, 446, 445, 444, 443, 443, 443, 443, 445]
; Construct a vector in which each element contains
; the year of the sample:
YEAR = [1967, 1970, INDGEN(9) + 1975]
```

If you prefer not to enter the data by hand, run the batch file `plot01` with the following command at the IDL prompt:

```@plot01
```

See Running the Example Code if IDL does not find the batch file.

The following IDL commands create a plot of the population of Sockeye salmon, by year:

```PLOT, YEAR, SOCKEYE, \$
TITLE='Sockeye Population', XTITLE='Year', \$
YTITLE='Fish (thousands)'
```

The PLOT procedure, which produces an x versus y plot on a new set of axes, requires one or two parameters: a vector of y values or a vector of x values followed by a vector of y values. The first attempt at making a plot produces the figure shown below.Note that the three titles, defined by the keywords TITLE, XTITLE, and YTITLE, are optional.

Axis Scaling

The fluctuations in the data are hard to see because the scores range from 428 to 463, and the plot's y-axis is scaled from 0 to 500. Two factors cause this effect. By default, IDL sets the minimum y-axis value of linear plots to zero if the y data are all positive. The maximum axis value is automatically set by IDL from the maximum y data value. In addition, IDL attempts to produce from three to six tick-mark intervals that are in increments of an integer power of 10 times 2, 2.5, 5, or 10. In this example, this rounding effect causes the maximum axis value to be 500, rather than 463.

The YNOZERO keyword parameter inhibits setting the y-axis minimum to zero when given positive, nonzero data. The figure below illustrates the data plotted using this keyword. The y-axis now ranges from 420 to 470, and IDL creates tick-mark intervals of 10.

```;Define variables:
@plot01
PLOT, YEAR, SOCKEYE, /YNOZERO, \$
TITLE='Sockeye Population', XTITLE='Year', \$
YTITLE='Fish (thousands)'
```

Multiline Titles

The graph-text positioning command !C, starts a new line of text output. Titles containing more than one line of text are easily produced by separating each line with this positioning command.

In the above example, the main title could have been displayed on two centered lines by changing the keyword parameter TITLE to the following statement:

```TITLE = 'Sockeye!CPopulation'
```

 Note
When using multiple line titles you may find that the default margins are inadequate, causing the titles to run off the page. In this case, set the [XY]MARGIN keywords or increase the values of !X.MARGIN or !Y.MARGIN.

Range Keyword

The range of the x, y, or z axes can be explicitly specified with the [XYZ] RANGE keyword parameter. The argument of the keyword parameter is a two-element vector containing the minimum and maximum axis values.

As explained above, IDL attempts to produce even tick intervals, and the axis range selected by IDL may be slightly larger than that given with the RANGE keyword. To obtain the exact specified interval, set the axis style parameter to one (`YSTYLE = 1`).

The effect of the YNOZERO keyword is identical to that obtained by including the keyword parameter `YRANGE = [MIN(Y), MAX(Y)]` in the call to PLOT. You can make /YNOZERO the default in subsequent plots by setting bit 4 of !Y.STYLE to one (`!Y.STYLE = 16`).

See STYLE for details on the STYLE field of the axis system variables !X, !Y, and !Z. Briefly: Other bits in the STYLE field extend the axes by providing a margin around the data, suppress the axis and its notation, and suppress the box-style axes by drawing only left and bottom axes.

For example, to constrain the x-axis to the years 1975 to 1983, the keyword parameter `XRANGE = [1975, 1983]` is included in the call to PLOT. The following figure illustrates the result.

Note that the x-axis actually extends from 1974 to 1984, as IDL elected to make five tick-mark intervals, each spanning two years. If, as explained above, the x-axis style is set to one, the plot will exactly span the given range. The call combining all these options is as follows:

```; Define variables:
@plot01
PLOT, YEAR, SOCKEYE, /YNOZERO, \$
TITLE='Sockeye Population', XTITLE = 'Year', \$
YTITLE = 'Fish (thousands)', XRANGE = [1975, 1983], /XSTYLE
```

 Note
The keyword parameter syntax `/XSTYLE` is synonymous with the expression `XSTYLE = 1`. Setting a keyword parameter to 1 is often referred to as simply setting the keyword.

Overplotting

Additional data can be added to existing plots with the OPLOT procedure. Each call to PLOT establishes the plot window (the rectangular area enclosed by the axes), the plot region (the box enclosing the plot window and its annotation), the axis types (linear or log), and the scaling. This information is saved in the system variables !P, !X, and !Y and used by subsequent calls to OPLOT.

Frequently, the color index, line style, or line thickness parameters are changed in each call to OPLOT to distinguish the data sets. The IDL Reference Guide contains a table describing the line style associated with each index.

The figure below illustrates a plot showing all four data sets. Each data set except the first was plotted with a different line style and was produced by a call to OPLOT. In this example, an (11, 4) array called ALLPTS is defined and contains all the scores for the four categories using the array concatenation operator. Once this array is defined, the IDL array operators and functions can be applied to the entire data set, rather than explicitly referencing the particular sample.

First, we define an n-by-4 array containing all four sample vectors. (This array is also defined by the `plot01` batch file.)

```ALLPTS = [[COHO], [SOCKEYE], [HUMPBACK], [CHINOOK]]
```

The plot in the preceding figure was produced with the following statements:

```; Define variables:
@plot01
; Plot first graph. Set the y-axis min and max
; from the min and max of all data sets. Default linestyle is 0.
PLOT, YEAR, COHO, YRANGE = [MIN(ALLPTS), MAX(ALLPTS)], \$
TITLE='Salmon Populations', XTITLE = 'Year', \$
YTITLE = 'Fish (thousands)', XRANGE = [1975, 1983], \$
/XSTYLE
; Loop for the three remaining scores, varying the linestyle:
FOR I = 1, 3 DO OPLOT, YEAR, ALLPTS[*, I], LINE = I
```