IDL 5220 Week 4 - Plots, Graphics, and Fitting

  1. PLOT:
    Plotting in IDL is very simple using the PLOT procedure: PLOT,[X,]Y[,options]. The x-axis array is optional and if omitted will default to array index number. So for a simple plot:
    Say you want to read in a FITS file and plot it with the correct x-axis scale:
    Any subset of an array can be plotted just as it can be used anywhere else: PLOT,x[500:700],flux[500:700].
  2. GatorPlot:
    GatorPlot (currently v4.4) is an interactive plotting package that I have written in IDL. Access it by typing gatorplot at an IDL prompt. It allows you to interactively plot one or more files at once, dynamically change options and labels, smooth plots, perform math operations, integrate flux, estimate the FWHM of emission lines, perform many types of fits, and more.
  3. Overplotting:
    You can overplot data with the OPLOT procedure. The only differences between it and plot are 1) it does not generate a new axis, but uses the scaling previously established by PLOT and does not erase anything and 2) it has less options accordingly. The form is OPLOT,[X,]Y[,options]. It will accept options such as PSYM, LINESTYLE, COLOR, MAX_VALUE, and MIN_VALUE, but not many others (see OPLOT in ? for full details). You can OPLOT as many times as you want on a single PLOT and new data is continually overlayed on top of the existing plot.
  4. Multiple Plots:
    You can put multiple plots on a page by setting the !p.multi system variable before creating your plots. !p.multi is a one dimensional array (or vector) of length 5. Set the first element almost always to 0. It controls the number of remaining empty sectors on a page, so 0 starts a new plot. The 2nd and 3rd elements contain then number of columns and rows respectively. The 4th column contains the number of plots stacked in the Z dimension and the fifth element controls the order in which the plots are drawn. 0 is the default (left to right). Set this to 1 if you want the order to be top to bottom (row major). Omitting any element is the same as setting it equal to 0, so usually just the first 3 elements are set. For example: to create a 6 panel plot with 3 columns and 2 rows:
    Once you have set !p.multi, use the PLOT procedure as you normally would. Each time the PLOT procedure is called, a new plot will be created in a new sector of the page. This is especially useful in FOR loops:
    for j=0,5 do begin
    Note that using the OPLOT procedure will NOT move the plot to the next sector. You can OPLOT as many times as you want on the first sector, then use PLOT to move to the next sector.
  5. Annotating with XYOUTS:
    Sometimes it is very useful to add text to the plot. Text can be added anywhere in the plot by the XYOUTS procedure: XYOUTS,[X,Y,]String. The X and Y coordinates can be either given in data coordinates (/DATA keyword) or normalized coordinates (/NORMAL). Data coordinates are useful when you want to place a label at a particular point that depends on your data values. Say you want to label an emission line at 1549 Angstroms, and flux of 5 on the y-axis:
    XYOUTS,1549,5,'C IV',/data
    Normalized coordinates are very useful in loops, and when you want to place text in say the top left corner of each plot. The plots are going to have different data values, so instead of bothering to find out each one, you can use a formula to include XYOUTS in your loop. Normalized coordinates go from 0.0 (bottom left) to 1.0 (top right) in both the X and Y directions. Adding the following line in the for loop shown above for the 6-panel plot will label the top left corner of each plot with the text contained in the string array label:
    XYOUTS,.05+.333*(j MOD 3),.95-(j/2),label[j],/normal
    Remember that since j is an integer, j/2 will also be an integer and drop the remainder. XYOUTS has several other optional parameters that can be set: CHARSIZE=value, CHARTHICK=value, ALIGNMENT={0.0 to 1.0}, COLOR=value, and ORIENTATION=value are 5 of the most common ones. An alignment of 0.0 aligns the left edge of the text with the given coordinate, 0.5 centers it, and 1.0 is right justified. Orientation should be set to the number of degrees counterclockwise from horizontal that you want the text to be rotated.
  6. Other types of Plots:
    There are of course many other types of plots possible in IDL besides regular X vs. Y plots. Many 3-dimensional plots are available as well. I will just touch on some of these, as there are again a lot of things you can do with each one.
  7. Outputting to Postscript Files:
    Outputting a plot to a postscript file is very easy. You just tell IDL before you make the plot to send it to the postscript device instead of the screen. This is done with 2 commands before you plot.
    Enter these two lines before you use PLOT, and after you are finished plotting (as well as using OPLOT and XYOUTS), enter the command
    to close and finalize the .ps file. You must remember the device,/close command or your .ps files will not come out right. If you want to then make plots to the screen you must tell IDL to send plots to the screen again by the command:
    The keyword device can take many options when you set the filename. These include specifying the size of the plot in inches as well as the margins:
    Make sure the /inches keyword is included. Other keywords that can be set include /ENCAPSULATED to create a .eps file and /LANDSCAPE to make a landscape file.

  8. Graphics:
    There are a lot of graphics and image processing tools available with IDL so again, I will just cover the basics because I haven't done too much with graphics.
  9. Fitting:
    IDL has many routines for least squares fitting of varying degrees of ease of use and applicability.

Homework 4: Fitting and Plotting a Spectrum. Write a program to fit a linear local continuum around the C III] 1909 emission line, and plot the spectrum with continuum drawn and the spectrum normalized by the continuum. Your program should do the following: 1) read the M7.fits file and use the CRVAL1 and CDELT1 values from the header to determine the x-axis wavelength scale. 2) Fit a local continuum using data in the wavelength ranges 1800-1820 and 2020-2040 angstroms. 3) create a 2-panel plot showing the spectrum with the continuum fit overplotted and the spectrum normalized by the continuum. Your plot should show the 1750 to 2050 Angstrom range (see below). 4) export this plot to a JPEG file. 5) Write the normalized spectrum (from 1750 to 2050 Angstroms only) back to a FITS file. Make sure you change the CRVAL1 line in the header to read 'CRVAL1 = 1750.0' since that is the new starting wavelength. And 6) Output a plot of the normalized spectrum to a .ps file (same as the bottom plot in the 2-panel plot).

When using XYOUTS to label the 2-panel plot, use /data for the top plot and /normal for the bottom plot. My program is 28 lines long. Label your plot as shown in the figure above. The tickmarks will be labeled automatically.