Basic Plotting in IDL

We've got most of the basic commands, now we just need to learn how to make a program and save our plots, and learn how to do a loop to make our life easier. Before we start, I'm going to give a description of one last basic command that will be very useful for making programs:

;     the semi-colon command is very important and allows you to "comment" your code, so you can leave notes about what you are doing a remove a line temporarely while you test the rest of your code. Anything after the semi-colon on the same line will not be read into the program.

First, to make a program, we open up a text file with emacs, gedit, or vi. The first line should be pro "name", and you must also specify an end to the program, so our last line should be "end". You should probably save the file as "name".pro so you know what it is exactly. So far, my program it looks like this:

Pro Idlplot
End

Now we have the beginnings of a program. Next, we put in all the commands that we had been typing into the command prompt into the file. We just want our readcol and our plot commands for now, and once we add those your program should look like this:

Pro Idlplot
readcol,'idlplot.dat',a,b,c
plot,a,b,title='B vs. A', xtitle='A',ytitle='B',color=250
end

Now we can test our program to see if everyting is working. To run a program, we must first compile it in IDL. To do this, we type in .r "filename".pro. This will compile the program. Now we just have to run it, to do this we just type the name of the program which is what we put after "pro" in our first line. This is why it is a good idea to name your file holding the program and the first line of the program the same thing, so you dont get confused. If all is working well, IDL should pop up a plot window and you should get a plot like on the previous page.

What we have done so far is great, but we need a way to save the plot. First we need to specify the type of file were goin gto save it as and a filename, we do this using the set plot and device command. The syntax can be a bit confusing, so I'll just give the format here for a ps/eps file:

set_plot,'ps'
device,filename='idlplot.eps'

This will set our file as a .ps file, and save it as the file name given in the device command. The device works is an open/close command, so we need to add a command to close it as well. The syntax is simply "device,/close". This should be added AFTER your plot command. I highly recommend reading the help in the device command, as this is where you can specify special font commands to use, or set the file to be landscape/portrait. There are a lot of extra commands you can add with it. Our program is almost finished, it should look something like this now:

pro Idlplot
readcol,'idlplot.dat',a,b,c
set_plot,'ps'
device,filename='idlplot.eps'
plot,a,b,title='B vs. A', xtitle='A',ytitle='B',color=250
device,/close
end

Now our program is done, and our plot is saved as a .eps file that can be used in open office or converted to a jpeg. The only thing we have left to do is learn about for-loops.

The for-loop is a useful tool that will help us save many lines of code. You give it an index range of numbers to run through, and specific tasks like the where command on theprevious page. An example