3. Direct Graphics

3.1. Plots

Plots are graphics drawn from vectors. These are accomplished very rapidly with IDL. For example this command

plot,sin(findgen(101)/100.*2.*!pi)

will already plot a decent sine wave on the screen. Generally IDL is very generous in providing reasonable default values if the user omits quantities.

The basic plot commands are

plot, x, [y] to plot a data set with axis and

oplot, x, [y] to over plot on an existing axis system.

If one or both axis are logarithmic, use the equivalent plot_io, plot_oi or plot_oo commands.
Plot commands establish a coordinate system and axis. The defaults may be overridden with keywords:

[x,y,z]range  specify axis limits
[x,y,z] style axis 'style'
[x,y,z]title  axis titles
[x,y,z]ticks axis ticks
title  plot title
position plot window position on output device
psym symbols for data points
 

To plot a line and/or symbols connecting points, use the

plots, x, y

command, where x,y are vectors holding the coordinates of the points to be connected. The default is plotting only the line, by setting keyword psym  you can add symbols and by setting line thickness to zero, suppress the line and plot only symbols.

IDL> x=findgen(51)/50.*2.*!pi
IDL> y=sin(x)
IDL> plot,x,y,psym=1,thick=0,xrange=[0,6.3],xstyle=1, $
     xtitle='phase', title='Sine Plot'

To annotate text to a plot you can use the

xyouts, xpos, ypos, string

routine.
 

3.1.1. Coordinate System

IDL allows most plotting commands to refer to positions using three different coordinate system:

1. Device coordinates counted in pixels from the lower left corner of the graphics are. They are, of course, extremely device depended and hence have to be handled with great care.

2. Normal coordinates normalize the size of the plot area to a range from 0. to 1.0 in each direction.

3. Data coordinates. After a coordinate system is established, data coordinates span the axis exactly as the plotted data.

Most IDL commands accept positions in any of the system, which have to be selected by a keyword /device, /data or /normal. A Coordinate transformation routine convert_coord is provided.
 

3.1.2. Multiple plots on one page

To plot data on a page or screen, IDL considers three virtual rectangles:

Each plot command calculates defaults for the orientation of the plot, but those positions can also be forced by setting system variables or using keywords.

Both variables use a vector of the form [xll,yll,xur,yur], where ll indicates the lower left and ur the upper right pixel that defines the rectangle.

The plot area can also be directly specified by the position keyword

IDL> x=findgen(101)/100.*2.*!pi
IDL> sx=sin(x)
IDL> cx=cos(x)
IDL> plot,sx,position=[.1,.6,.9,.9],/noerase
IDL> plot,cx,position=[.1,.1,.9,.4],/noerase

IDL provides a shortcut for multiples plots on one page by the use of the !p.multi system variable. !p.multi is a three element vector [i,n,m] with the following meaning:

i: number of remaining plots before erasing page. Has to be zero at the beginning of a multiple plot.
n: number of horizontal sub screens
m: number of vertical sub screens

We can obtain roughly the same result as above with:

IDL> !p.multi=[0,1,2]
IDL> plot,sx
IDL> plot,cx
The advantage of this form of course is that we do not have to worry about where to place the subplots, IDL will position them automatically in a decent manner.
 

3.1.3. Fonts

The !p.font variable controls which kind of font is applied by IDL. Basically there are two options: hardware generated fonts, as supplied by a printer or terminal, or vector drawn fonts, which are internal IDL fonts constructed from vector strokes.

The default is to use vector fonts on terminals and hardware fonts on postscript devices. If the appearance of a printout has to match the screen view,  switch back to vector fonts while printing on postscript.

Internal vector fonts come in a variety of forms that can be switched within a plot string by using the ! as an escape character (!! will print the exclamation mark). For instance !9 will switch to mathematical symbols. (Do not use !m instead of !9 with IDL5.1 since it is buggy.) See the manual for the 20 fontsets.

Special positioning commands are also available using the exclamation mark.

Command Meaning 
!a shift above division line 
!b shift below division line 
!c carriage return and new line 
!d subscript 
!e exponent 
!i index 
!l second level subscript 
!n back to normal level and size 
!r restore position (see !s) 
!s save position (stacked) 
!u upper subscript 

subscript, upper subscript and second level subscript have the character size multiplied by 0.62, while index and exponent have them multiplied by 0.44.

xyouts,.1,.3,size=5,/norm,'!9I!5sin!9(!s!3-!r!a!s!5x!e2!n!r!b!7u!n!9)'
Exercise: Plot Maxwell's equations
Exercise: Plot the definition of a Gaussian
 

3.2. Directing Graphic Output to a Printer

In order to direct graphic output to a printer instead of a terminal window, use the device command with appropriate options, do the graphics and close it, again using device.

To facilitate this actions appropriately, the IAC provides to library functions called psinit and psterm. To open a printer as a graphics device use

psinit, printer, [keywords]

where printer is a character string with the printer name (e.g. lw1). Default printing will be in portrait orientation on upper half of page. Available keywords include:

full print on entire page 
landscape print in landscape mode 
vector use vector drawn fonts 
double double stroke on fonts (improves quality) 
color produce color postscript 
encapsulated produce encapsulated postscript 

After performing all graphics commands close the plot by

psterm [,keywords]

If no keywords are specified, all graphics will now be send to printer. To suppress immediate printing, use the noplot keyword. Use the keyword file=<filename> to direct output into a file. If not used in conjunction with /noplot, plot will equally be saved to file as well as printed.

3.3. Displaying images

Images are 2 dimensional data sets. To visualize them, a pixel at the output device is given the value of the corresponding data pixel. In this course we will only consider 8 bit image visualization. As the depth of the screen buffer then is 1 byte, it is recommendable to normalize image pixel to the range of 0-255. This is automatically done when using the routine

tvscl, image

If you prefer your scaling, perform it and use

tv, scaled_image

for placing the image on the screen, use the methods than with plots.

IDL comes with 18 predefined color tables, if the initial one does not serve you, you can either define your own one with tvlct or select one of the predefined with loadct, <n> or interactively with the

xloadct

command. This opens a graphical interface to select and modify the predefined color tables.

3.4. 8 bit versus 24 bit graphics, color tables

24 bit graphics cards represent the color of each pixel by one byte for the fraction of the red, green and blue part. They can produce several million of different colors and hence are indistinguishable from a continuous color spectrum.

red green blue
0 0 0
1 1 1
2 2 2
3 3 4
... ... ...
... ... ...
255 255 255
RGB 24bit color system
 

Each color in this system is represented by a byte triplett. The value (128,128,128) hence would represent a medium dark gray, while (129,128,128) would add a slight touch of red to it.

8 bit or mapped colors use a color table to map the values from 0-n>255  (8 bit) to a certain color. The image itself only consists of the color index values (usually  8bit).
Almost all workstations at the IAC use (cheaper) 8 bit graphics cards rather than 24 bit 'true color' graphics.

color index red green blue
0 4 19 7
1 17 122 200
2 280 18 201
3 33 139 13
... ... ...
... ... ...
n 100 255
8bit pseudo color system
 

The window system establishes a color table with a only a few colors defined. Applications usually share these color table for their grafical user interface. Visualization software like IDL now also needs colors to represent images. By default, IDL tries to get hands on all colors (minus a few) and apply an own color table on them. In this case there will be only one color table on the workstation.

It is allways posible to convert an image from 8bit to 24bit true color system. The reverse is obviously not true.

It may be that one very few or no colors are available (for instance if a web browser already uses them). In this case, IDL will start its graphics windows with proper color tables of length 256.
The user notices this by a change in color table if the mouse pointer is inside or outside an IDL window.

In order to have reproducable colors, it may be desirable to start IDL in the proper color table mode. Use

window,colors=256

when opening the first graphic window to force private 8bit color maps for each window.

Sometimes printouts do not show the same colors displayed in a window. To some part this is unavoidable, since printers must use a subtractive color sceme, will monitors have additive colors. Also printers may not be able to produce any color or gray level.
Best match usually is obtained when private color maps on the screen are used.
 

3.5. Plotting two dimensional data

3.5.1. Contours

The easiest way to visualize data depending on two independent variables is by contours.

contour, arr, [x, y]

plots contour lines of an array arr. If x and y vectors for axis scaling are not given, contour will use element index numbers.

It is fairly easy to over plot images and contours, we will do it by an example:

arr=exp(smooth(dist(20),3)/3.) ; the data
!p.position=[.1,.1,.9,.9] ; define plot region
!p.region=!p.position
devreg=convert_coord([.1,.9],[.1,.9],/norm,/to_dev) ; convert region to device coords
darr=resize(arr,devreg(0,1)-devreg(0,0),devreg(1,1)-devreg(1,0)) ; resize the array
tvscl,darr,!p.region(0),!p.region(1),/norm ; show it
contour,arr,/noerase  ; over plot the contours
loadct,4 ; some fancy colors

3.5.2 Surfaces

Contrary to contour plots, a surface plots define a three dimensional axis system. The surface routine defines a transformation of data to a default axis system. It can be used for further work on the plot.

surface, arr, <x, y>
shade_surf, arr, [x, y>

work the same way than contour and yield either a mesh wire surface, or a shaded surface of the data. If used with the save keyword, the transformation matrix (!p.t) is saved and can be used by other routines, such as plot or contour by specifying the t3d keyword. An example will clarify the possibilities.

arr=exp(smooth(dist(20),3)/3.) ; the data
loadct,3 ; decent reddish colors
shade_surf, arr, /save ; plot shaded surface and save matrix
surface, arr, /noerase ; over plot the mesh wire
contour, arr, /t3d, zvalue=15, levels=findgen(max(arr)/10.)*10., /noerase ; a contour plot 
    at top of z-axis
contour, arr, /t3d, zvalue=0, /noerase ; a contour plot in the xy plane
xyouts,5,-3,'My data',/t3d, text_axes=0, charsize=5 ; annotate some text
xyouts,10,33,'this goes up',/t3d, text_axes=5, charsize=5 ; text parallel z-axis
using tv to over plot the data in this case is more difficult, since the data have to be transformed manually. See the manual for that, or study the user routine show3 for a programming example.

3.6. Catching the cursor

To determine cursor position IDL provides the

cursor, x, y, switch

command. It will store the current cursor position (in device, normal or data coordinates, like selected with keywords) to the x and y variables. By default, the coordinates are immediately read out, unless the optional parameter switch demands something else. It can have the following values:

wait wait until a button is down 
change wait until a movement is performed or a button pressed or released
up wait until a button is released 
down wait until a button is pressed 
nowait read immediately

Information about which mouse button has been used (if) any is stored in the !err variable. A value of 1 corresponds to the left, 2 to middle and 4 to the right button.