next up previous contents
Next: Resetting, compiling, saving etc Up: Lecture 1: Getting Started Previous: Other IDL notes   Contents

A First Plot

Now that we have IDL working, let's make a plot with it. Let's suppose that we have several points (each of which has horizontal and a vertical co-ordinate) and that we want to plot them on a graph. We'll be traditional and call the horizontal co-ordinates $x$ and the vertical co-ordinates $y$. We can enter the data into IDL like this.

 
IDL> x = [1, 2, 4, 5, 6.7, 7, 8, 10 ]
IDL> y = [40, 30, 10, 20, 53, 20, 10, 5]

We now have two array variables called $x$ and $y$, each containing eight numbers Do ``help,x'').

 
help,x  ; and just try
help
to test that this is true. To make a plot of $x$ against $y$, we just type this:

IDL> plot,x,y
and there it is. It is a bit plain, but we have visualised our data. No-one else is going to know what it means unless we label the axes, like this:

plot,x,y,title='Should he resign?',xtitle='weeks',ytitle='Popularity'
Annoyingly the plot in the graphics window will disappear if you cover it up. Avoid this by using
IDL> window,0,retain=2
I got fed up with this that I defined a procedure w so that w,0 does this.

We suppose that the data is the popularity ratings of a politician as determined by a polling organisation. Notice that there are two different ways in which we have provided instructions to the `plot' routine. The data, x and y, are provided as positional parameters. The order they come in matters. Here, the first parameter is the horizontal co-ordinates of our data, the second is the vertical co-ordinates. Optional things like the labels are passed as keyword parameters. An important IDL routine like plot usually has many of these. You can supply them in any order you like.

Now let's suppose that our politician's popularity is being measured by two polling organisations. We put both sets of measurements on the same plot by using plot for the first set and oplot for the second.

IDL> y2 = [30, 28, 8, 19, 50, 22, 12, 6]
IDL> oplot,x,y2,linestyle=2

Note how we use the linestyle keyword to make the second line different from the first.

This is getting to the stage where it might be tedious to re-type everything to correct a mistake we made earlier. We can avoid this by putting a list of IDL commands into a file to make a program. As an example, use your favourite text editor to create a file called dubya.pro and put the following lines in it:

;+
;pro dubya
;Procedure to plot dubya's polularity over time as polled by IAS and
;SRI
;No parameters or keywords
;Hugh Pumphrey, Edinburgh (many years ago). Updated: John Marsham,
;Leeds (16/3/05)
;-

; **** Here are the data. ****
x = [1, 2, 4, 5, 6.7, 7, 8, 10 ]    ; time 
y = [40, 30, 10, 20, 53, 20, 10, 5] ; popularity  (IAS polling)
y2 = [30, 28, 8, 19, 50, 22, 12, 6] ; popularity  (SRI polling)


; ****  make a plot of the first set of data ***** 
plot,x,y,title='Should he resign?',xtitle='weeks', $
   ytitle='Popularity', psym=-2	

; --- Note how we use $ in the above command  to continue onto the
; --- next line

; +++++++  Add the second set to the plot +++++++ 
oplot,x,y2,linestyle=2, psym=-6

; End of the program - don't forget this!
end

Note that the semicolon is used to indicate a comment - IDL ignores everything on a line after the first semicolon.

Note that the dollar symbol, $, is a line continuation character

Note the introduction between the ;+ and ;-. This is standard - it's really useful to define all the inputs, outputs, keywords etc here (including units!) You can automatically document all these introductions using

``MK_HTML_HELP''.

It is important that the file name ends in ``.pro'' or IDL may not find it. Also using capitals in the procedure name can cause problems. The file also has to be in the directory from where you started IDL (unless you have updated your .cshrc. If you have never used IDL before, why not take this program, run it, and then make some changes to it. Read up on the `plot' routine in the manual and find out how to do things like change the axis ranges, use logarithmic axes, plot the lines in colour etc. etc.


next up previous contents
Next: Resetting, compiling, saving etc Up: Lecture 1: Getting Started Previous: Other IDL notes   Contents
John Marsham 2005-04-22