THE IDL plot COMMANDS for BEGINNERS (IDLPLOTCOMS)

Carl Heiles^{1} October 18, 2009 This is an introduction to the IDL plot command for the computer terminal (X window). To

make a PostScript file for insertion into a Latex document, see §7 and our handout *****. This document is only an introduction: you can do almost anything with the plot command! See the on-line documentation.

1. The plot Command

We can call plot with several keywords and optional inputs and these are what we’ll explore here. Figure 1 is the plot generated by the following command: (Note that the $ symbol tells IDL that the command is continuing on the next line)

IDL> plot, xdata, ydata, psym=7, $
xrange=[0,10], yrange=[0,20], /xstyle, /ystyle, $
xtitle='x-axis label (units)', $
ytitle='y-axis label (units)', $
title='Title for entire plot'

Fig. 1.— An example plot.

1Original by Josh Walawender in 2002

Now we can look at the properties of Figure 1 and see what commands created them. First we should note that by prepending a / onto an IDL keyword we are setting that keyword’s value to one—many keywords simply look to see if it has a value or not, a sort of true/false setting.

xdata and ydata: the data arrays which contain the data to be plotted on the x and y axis. psym=4: sets the data symbol. By setting this to 7 we select the × symbol to represent the

data. You can select other numbers to use other symbols (i.e. 3=dots, 4=squares, 5=triangles, 6=squares, 7=×s, 10=histogram style). When psym is set to a negative number, then the data points are connected by lines.

(xy)range: setting these equal to a two element array causes the plot ranges to be set to the values in those arrays. For x, the (first, second) elements are the (left, right) axis limits and for y, the (bottom, top).

/(xy)style: Normally, the x and y ranges are rounded to make human-comfortable numbers (like 10 instead of 9.5). Setting these styles to unity forces the ranges to be exactly what you specify. There are lots of other options for the style keywords.

(xy)title: set this to a string which becomes the title for that axis or for the plot itself.

2. Overplotting with the oplot command

We can also use the oplot command to overlay other plots on top of ones we have already made. For example, if we wanted to place a dashed line representing the theoretical model through our data (see Figure 2) we would use the following command:

IDL> oplot, xdata, ytheory, linestyle=2

linestyle: sets the linestyle. By setting this to 2 we selected a dashed line. Other numbers represent, for example, a dotted line or a dash-dot line. (linestyle also works in plot).

ytheory: the array of theoretical values calculated for y.

3. Plotting Error Bars with oploterr

We can add also error bars to our plot using the oploterr command. In our example we can plot the y-axis uncertainty (see Figure 3) using the following command:

IDL> oploterr, xdata, ydata, yerrors

yerrors: the array of errors on the y measurements.

There are other ways to plot error bars. See, for example, errplot and ploterr .

Fig. 2.— An example plot with an additional plot overlayed. Fig. 3.— An example plot with the error bars plotted.

4. The plots command The plots command allows you to plot individual point, or an array of points, all with different

colors and/or symbols.

5. Annotate and Create Plot Legends with the xyouts command

The xyouts command lets you annotate plots. In the example below, xyouts, 7.0, 6.0, 'data points' writes the string data points beginning at the specified locations of (x,y) = (7.0, 6.0).

You can add legends to your plots using plots and xyouts. The legends in Figure 4 were made with the following commands:

IDL> plots, 6.8, 6.1, ps=7
IDL> xyouts, 7.0, 6.0, 'data points'
IDL> plots, 6.5,6.9,5.5,5.5, lines=2
IDL> xyouts, 7.0, 5.4, 'theory'
IDL> plots, 6.8,6.8,4.6,5.1
IDL> xyouts, 7.0, 4.8, 'error bars'

That’s the hard way! The easy way: use GSFC’s legend command. To see its documentation,

type doc, 'legend' in IDL.

Fig. 4.— An example plot with a legend.

5.1. You Want Greek Letters or Math? Use textoidl

There are two ways to do Greek letters and sub/superscripting. One is IDL’s native embedded formatting, which is infinitely flexible and consequently complicated; the other is the popular nonnative procedure textoidl, which lets you use TEX notation to generate fancy IDL characterstrings. For example, suppose we want to annotate a graph with the string Density^{2}/Radius_{1}^{3} ≤ γ. Using textoidl you’d write

xyouts, xloc, yloc, textoidl('Density^2 / Radius_1^3 \leq \gamma'

If this looks like Greek to you, then you don’t know TEX. As a scientist, you’ll need to learn it!

6. Making Arrays of Plots using the !p.multi System Variable

The !p.multi system variable in IDL allows you to place multiple plots into the same window^{2}. To accomplish this, set !p.multi equal to an array with the first element zero, the second element equal to the number of plots across the window and the third element equal to the number of plots down the window. Then give your plotting commands. For example, to place six plots in one window with two across and three down:

idl> !p.multi=[0,2,3]
idl> plot, x1, y1
idl> plot, x2, y2
idl> plot, x3, y3
idl> plot, x4, y4
idl> plot, x5, y5
idl> plot, x6, y6
idl> !p.multi=[0,1,1]

Always remember to set !p.multi back to [0,1,1] or equivalently, to 0, when you’re done; otherwise your next window will have places for six plots.

!p.multi allows enough space around each plot for its own axis titles, and a plot title. Often, the x-axes and/or the y-axes have identical titles or scales. In such cases, the space around each plot is annoying and wasteful. You can eliminate it by using GSFC’s multiplot.pro. Check its documentation.

7. Turning Your Plots into Postscript Files

There’s an art—based human physiology, document printing technology, and PowerPoint technology—to making images that are easily read and absorbed into the human psyche. It includes using easily-readable fonts, thick lines, and contrasting colors that are distinguishable even by the colorblind^{3}. IDL makes exportable images in many formats, but the one you’ll use for most purposes is PostScript. It’s easy to make great PostScript images and plots in IDL! See our document entitled ******.

2!p.multi is an IDL system variable because it is prepended by a ! . System variables are always accessible, whether you are at the main level or in any procedure or function. Some other noteworthy system variables are !pi, !dtor, !radeg .

3Whose numbers, especially among males, are a surprisingly high ∼ 15% .