Primeros pasos in IDL

4. Visualization

4.1. Windows

By default your plots are displayed on the IDL graphics windows. If you wish to create a window type:

WINDOW [, Window_Index] [, COLORS=value] [, /FREE] [, /PIXMAP] [, RETAIN={0 | 1 | 2}] [, TITLE=string] [, XPOS=value] [, YPOS=value] [, XSIZE=pixels] [, YSIZE=pixels]

Of course, you do not need to specify all these keywords, but you have a number of possibilities described in the examples.


;to create a window type
IDL> window
;to create a second window type
IDL> window,1
;if you type once again window,1 the current window will be destroyed and created once again.

;To check which window is the one currently active
IDL> whichwindow=!D,Window & print,whichwindow

;If you wish to plot on a certain window, for example, window 5:

;To delete window 5:
;If you wish to delete all the window you have currently opened there is a trick:
IDL> WHILE !D.Window ne -1 do wdelete, !D.Window ;since !D.Window=-1 if no graphics window are created

;You can change the size of your window like this:
IDL> window,1,XSIZE=256,YSIZE=256

;To show up your hidden window
IDL> WSHOW,5 ;if you don't specify a number then the current window will be shown up

;To erase the contents of the current window:

;If you wish to change the color of your window
IDL> ERASE,COLOR=250 ;this will make your window to look white if you had not changed your default color palette

4.2. Plotting

Plotting vectors is very simple in IDL, which allows you a large number of possibilities. The most important command is:

PLOT, [X,] Y [, MAX_VALUE=value] [, MIN_VALUE=value] [, NSUM=value] [, /POLAR] [, THICK=value] [, /XLOG] [, /YLOG] [, /YNOZERO]



;Suppose you wish to plot a sine function:
IDL> X=FINDGEN(21)*2*!PI/20

For adjusting the axes you should use the XSTYLE keyword and set it to 1. Other possible values for this keyword are 2 to extend the axis range, 4 to supress the entire axis, 8 to suppress the box style.


You can change line style by using the keyword LINESTYLE. The following values are allowed: ;0=Solid, 1=Dotted, 2=Dashed; 3=Dash Dot,  4=Dash Dot Dot , 5=Long Dashes

IDL> PLOT,X,SIN(X),XSTYLE=1,LINESTYLE=3 ;to plot a dot dashed line

To add the axis labels you should use XTITLE and YTITLE as well as TITLE for the title of your plot.

IDL> PLOT,X,SIN(X),XSTYLE=1,LINESTYLE=3,TITLE='sin(x) plot', XTITLE='angle',YTITLE='sin(x)'

To plot symbols instead of lines you should use PSYM. A number of symbols are available when PSYM is set from  1 to 7.  For example 1 means +, 2 means *, 7 is x, etc. When PSYM is set to 10 data is plotted in histogram mode.

IDL> PLOT,X,SIN(X),XSTYLE=1,PSYM=6,TITLE='sin(x) plot', XTITLE='angle',YTITLE='sin(x)' ;squares
IDL> PLOT,X,SIN(X),XSTYLE=1,PSYM=10 ;histogram

If you wish to connect symbols with line you should set PSYM to a negative value:

IDL> PLOT,X,SIN(X),XSTYLE=1,PSYM=-5 ;triangles with line

The symbol size can be changed with SYMSIZE

IDL> PLOT,X,SIN(X),XSTYLE=1,PSYM=-4,SYMSIZE=3.0 ;diamond shaped symbols will be 3 times larger

You can create your own symbol with USERSYM procedure. To plot your symbol you need to set PSYM to plus or minus 8. Symbols can be drawn with vectors or can be filled. Symbols can be of any size and can have up to 50 vertices. Its syntax is:

USERSYM, X [, Y] [, COLOR=value] [, /FILL] [, THICK=value]

The X and/or Y parameters define the vertices of the symbol as offsets from the data point in units of approximately the size of a character. In the case of a vector drawn symbol, the symbol is formed by connecting the vertices in order.

IDL> a = [0.0, 0.5,-0.8,0.8,-0.5,0.0]
IDL> b = [1.0,-0.8,0.3,0.3,-0.8,1.0]

To change the range of line plots you should use XRANGE and/or YRANGE.

IDL> PLOT,X,SIN(X),XSTYLE=1,XRANGE=[0,2] ;Only the x values in this range will be plotted

If you wish to reverse an axis, you only need to change the order of your limits in XRANGE and/or YRANGE.


To play around with tick marks you have several routines such as TICKLEN, XTICKLEN,TICKS,XMINOR.

To enlarge the size of your labels you should use CHARSIZE.

IDL> plot,x,sin(x),xstyle=1,linestyle=0,xtitle='x',ytitle='sin(x)',CHARSIZE=2.5

Check the IDL help for exploiting the full capabilities offered by all these keywords.

4.2.1. Overplotting and adding symbols and lines

For overplotting data on your plot you can use OPLOT. This command does not generate a new axis, which is established by the most recent PLOT call. The syntaxis is as follows:

OPLOT, [X,] Y [, MAX_VALUE=value] [, MIN_VALUE=value] [, NSUM=value] [, /POLAR] [, THICK=value]

Graphics Keywords accepted by this procedure are: [, CLIP=[X0, Y0, X1, Y1]] [, COLOR=value] [, LINESTYLE={0 | 1 | 2 | 3 | 4 | 5}] [, /NOCLIP] [, PSYM=integer{0 to 10}] [, SYMSIZE=value] [, /T3D] [, ZVALUE=value{0 to 1}]


IDL> x=findgen(21)*2*!PI/20
IDL> y=findgen(21)*4*!PI/20
IDL> plot,x,sin(x),xstyle=1,linestyle=0
IDL> OPLOT,y,sin(y),linestyle=2

You can use the PLOTS procedure for drawing lines and symbols on your current plot. The syntaxis is:

PLOTS, X [, Y [, Z]] [, /CONTINUE]

The CONTINUE keyword is set to continue drawing a line from the last point of the most recent call to PLOTS. The following optional graphics keywords are available: [, CLIP=[X0, Y0, X1, Y1]] [, COLOR=value] [, /DATA | , /DEVICE | , /NORMAL] [, LINESTYLE={0 | 1 | 2 | 3 | 4 | 5}] [, /NOCLIP] [, PSYM=integer{0 to 10}] [, SYMSIZE=value] [, /T3D] [, THICK=value] [, Z=value]


IDL> x=findgen(21)*2*!PI/20
IDL> plot,x,sin(x),xstyle=1,linestyle=0
IDL> xvalues=[0,6]
IDL> yvalues=[0,0]
IDL> PLOTS,xvalues,yvalues,linestyle=1
IDL> xvalue2=1
IDL> yvalue2=-0.5
IDL> PLOTS,xvalue2,yvalue2,psym=4,symsize=2.5

4.2.2. Annotation

You might write a text on your plot by using XYOUTS procedure. The syntaxis is as follows:

XYOUTS, [X, Y,] String [, ALIGNMENT=value{0.0 to 1.0}] [, CHARSIZE=value] [, CHARTHICK=value] [, TEXT_AXES={0 | 1 | 2 | 3 | 4 | 5}] [, width=variable]

Particularly useful here are the keywords ORIENTATION for writing the text following a given angle, and ALIGNMENT, which specifies the alignment of the text baseline. An alignment of 0.0 (the default) aligns the left edge of the text baseline with the given (x, y) coordinate. An alignment of 1.0 right-justifies the text, while 0.5 results in text centered over the point (x, y).


IDL> x=findgen(21)*2*!PI/20
IDL> plot,x,sin(x),xstyle=1,linestyle=0
IDL> XYOUTS,1,-0.5,'This is a sin function',orientation=45
IDL> XYOUTS,1,-0.5,'This is a sin function',orientation=45,alignment=1

The ANNOTATE procedure starts an IDL widget program that allows you to interactively annotate images and plots with text and drawings. The syntaxis is:

ANNOTATE [, COLOR_INDICES=array] [, DRAWABLE=widget_id | , WINDOW=index] [, LOAD_FILE=filename] [/TEK_COLORS]


IDL> ; Output an image in the current window:
IDL> plot,x,sin(x),xstyle=1,linestyle=0
IDL> ; Annotate it:
IDL> ANNOTATE ;after deciding your options clik on the window and save your changes and also your file if desired. To exit make use of the "File" option in the main menu.

IDL provides a  large dataset of fonts. The !P.FONT variable tells IDL the font type to be written. When you are plotting a string you switch to the required font type by typing !. For example !7 specifies complex greek fonts., whereas !3 is the default simplex roman, !9 is for Math fonts. Check the IDL help for further fonts. A useful command is SHOWFONT, which displays the requrested vector-drawn font.. Some interesting codes for positioning are !a for shifting above division line, !b shift below division line !c carriage return, !d subscript, !l second level subscript, !e exponent, !u upper subscript.

There is a trick to see the full set of true type font names on the system:

IDL> device,font='*',Get_fontnames=fontnames, /tt_font
IDL> for j=0,N_elements(fontnames)-1 do print,fontnames(j)


IDL> SHOWFONT, 'Helvetica Italic', 'Helvetica Italic', /TT_FONT ;to see the whole set of fonts

IDL> erase
IDL> xyouts, 0.5,0.5, /normal,size=3, 'A!dB!n(mag)'
IDL> erase
IDL> xyouts, 0.5,0.5, /normal,size=3, 'Y=3x!U2!N+5x+3'

4.2.3. Multiple plots on a single page

!P.MULTI system variable allows you to perform multiples plots on a single page. It is a 3 element vector [i,n,m]:

i is the number of remaining plots before erasing page (i=0 when begining a multiple plot)

n number of horizontal sub screens

m number of vertical sub screens

You return to a single plot mode by typing



IDL> !p.multi=[0,2,2]
IDL> x=findgen(21)*2*!PI/20
IDL> plot,x,sin(x)
IDL> plot,x,cos(x)
IDL> plot,x,sin(x)/cos(x)
IDL> plot,x,cos(x)*sin(x)
IDL> !p.multi=0

A full control on the location of the plots is allowed by making use of the POSITION keyword within the PLOT command. POSITION is a 4-element vector giving, in order, the coordinates [(X0, Y0), (X1, Y1)] (in normalized units ranging from 0.0 to 1.0) of the lower left and upper right corners of the window.  You must allow space for the annotation, which resides outside the window, otherwise an error message will warn you.

Sometimes it would be interesting to set to a very small value the XCHARSIZE or YCHARSIZE keywords of the PLOT command to allow a single axis to be shared by two plots.


IDL> ;please write and execute the following procedure called ''

>pro position_plot

4.2.4. Colors

Adding colors to your plots is very simple in IDL. IDL works in two different modes with colors:
  If you do not know whether your graphic card is 8 or 24 bits you can type the following commands:

IDL> device,get_visual_name=n,get_visual_depth=d

IDL> print,n,d

If you have a 8-bit graphic card you only can use the indexed model, and the option DEVICE, DECOMPOSED=1 is not allowed. Alternatively, if you have a 24-bit graphic card you could work as if you were using a 8-bit machine by setting DEVICE,DECOMPOSED=0.

You can define and load your colors with TVLCT by means of three color vectors that describe red, green and blue components (although other options are allowed as well). This is a very good approach when you only need a few colors.


IDL> tvlct,[255,0,255,0,0],[255,255,0,0,0],[255,0,0,255,0],1
IDL> ;you have created 5 colors starting at number 1 (white: 255,255,255) and ending at 5 (black: 0,0,0)
IDL> device,decomposed=0
IDL> !p.multi=[0,2,2]
IDL> plot,hanning(21),color=1 ;hanning is a predefined function
IDL> plot,hanning(21),color=2
IDL> plot,hanning(21),color=3
IDL> !p.multi=0
IDL> plot,hanning(21),color=4,background=3 ;you are changing your background
IDL> plot,hanning(21),color=4,background=5

The system has a predefined set of palettes that you can load with LOADCT command. You need to type the number of the required palette.  XLOADCT utility provides a graphical widget interface to the LOADCT procedure. Moreover you can make use of the more clever utility XCOLORS, which belongs to the library "Coyote" of D. Fanning (which has been installed in our local libraries directory).


IDL> device,decomposed=0
IDL> xloadct ;choose one and save it
IDL> plot,hanning(21),color=50
IDL> plot,hanning(21),color=150
IDL> plot,hanning(21),color=250
IDL> loadct,0 ;returns to the black and white linear default palette.

4.3. Generating your postscript files

To generate a posctript file you have to type:


To enable color output you might use:


or type an additional statement:


To increase the number of colors in your postctript file make use of BITS_PER_PIXEL keyword of the DEVICE command and set it to 8:


You can decide the size of your output with the DEVICE keywords XSIZE and YSIZE and the offset keywords XOFFSET, YOFFSET. Finally, you should close your postcript file with


To return to the default output:



IDL> set_plot,'ps'
IDL> device,filename='',xsize=17,ysize=25,yoffset=1.
IDL> plot,hanning(21)
IDL> device,/close
IDL> set_plot,'x'

4.4. Array visualization

Surfaces can be displayed with a grid using the SURFACE procedure. Its syntaxis is:

SURFACE, Z [, X, Y] [, AX=degrees] [, AZ=degrees] [, BOTTOM=index] [, /HORIZONTAL] [, /LEGO] [, /LOWER_ONLY | , /UPPER_ONLY] [, MAX_VALUE=value] [, MIN_VALUE=value] [, /SAVE] [, SHADES=array] [, SKIRT=value] [, /XLOG] [, /YLOG] [, ZAXIS={1 | 2 | 3 | 4}] [, /ZLOG]

SURFACE accepts all graphic keywords accepted by PLOT except  PSYM and SYMSIZ. Rotations are controlled by AX and AZ keywords.

An alternative is to use SHADE_SURF procedure. Its syntaxis is:

SHADE_SURF, Z [, X, Y] [, AX=degrees] [, AZ=degrees] [, IMAGE=variable] [, MAX_VALUE=value] [, MIN_VALUE=value] [, PIXELS=pixels] [, /SAVE] [, SHADES=array] [, /XLOG] [, /YLOG]


IDL> SURFACE, dist(21)
IDL> SURFACE, dist(21),AZ=-20,AX=20

IDL> SHADE_SURF, dist(21)

Countours can be displayed with CONTOUR. Its syntaxis is:

CONTOUR, Z [, X, Y] [, C_CHARSIZE=value] [, C_CHARTHICK=integer] [, C_COLORS=vector] [, C_LABELS=vector{each element 0 or 1}] [, C_LINESTYLE=vector] [{, /FILL | , /CELL_FILL} | [, C_ANNOTATION=vector_of_strings] [, C_ORIENTATION=degrees] [, C_SPACING=value]] [, C_THICK=vector] [, /CLOSED] [, /DOWNHILL] [, /FOLLOW] [, /IRREGULAR] [, LEVELS=vector] [, NLEVELS=integer{1 to 60}] [, MAX_VALUE=value] [, MIN_VALUE=value] [, /OVERPLOT] [{, /PATH_DATA_COORDS, PATH_FILENAME=string, PATH_INFO=variable, PATH_XY=variable} | , TRIANGULATION=variable] [, /PATH_DOUBLE] [, /XLOG] [, /YLOG] [, /ZAXIS] [, /ZLOG]


IDL> Z = DIST(100)
IDL> CONTOUR, Z, NLEVELS=10, /FOLLOW, TITLE='Simple Contour Plot'

Images can be displayed with TV and TVSCL. The TVSCL procedure scales the intensity values of Image into the range of the image display and outputs the data to the image display at the specified location.


IDL> TV,dist(256)
IDL> TVSCL,dist(256)

4.5. Volumes

Volumes are visualized with SHADE_VOLUME and POLYSHADE commands. SHADE_VOLUME produces a list of vertices and polygons describing the contour surface. This surface can then be displayed as a shaded surface by the POLYSHADE procedure. Shading is obtained from either a single light-source model or from user-specified values. The syntaxis is:

SHADE_VOLUME, Volume, Value, Vertices, Polygons [, /LOW] [, SHADES=array] [, /VERBOSE] [, XRANGE=vector] [, YRANGE=vector] [, ZRANGE=vector]


Result = POLYSHADE( Vertices, Polygons)


Volume is the 3D array to be plotted

Value is the isosurface scalar value to be rendered

Vertices is the name of the variable with the vertex array to be created

Polygons is the name of the variable with the list of polygons to be created

SCALE3 is a useful procedure to be able to set up a 3D transformation and scaling parameters for the 3D viewing. Its syntaxis is:

SCALE3 [, XRANGE=vector] [, YRANGE=vector] [, ZRANGE=vector] [, AX=degrees] [,AZ=degrees]

where XRANGE, YRANGE and ZRANGE represent the data ranges in the corresponding directions, and the AX and AZ keywords indicate the rotations about the X and Z axes respectively.

For example the following procedure will show you a head:

IDL> fichero=filepath('head.dat',SUB=['examples','data'])
IDL> openr,1,fichero
IDL> cabeza=bytarr(80,100,57)
IDL> readu,1,cabeza
IDL> close,1
IDL> loadct,9
IDL> window, retain=2
IDL> ;Set up a 3D transformation where the data range is the same as in the bynary file for each of the 3 axes and the viewing area is rotated 0 degrees about the X and Z axes:
IDL> scale3,xrange=[0,79],yrange=[0,99],zrange=[0,56],ax=0,az=0
IDL> ;to be able to plot the isosurface corresponding to the mean value of the array:
IDL> SHADE_VOLUME,cabeza,MEAN(cabeza),v,p
IDL> tvscl,POLYSHADE(v,p,/T3D)

For dumping into a file the obtained image you can make use of the Fanning routine TVREAD that we have installed in our local libraries directory. This routine is much better than TVRD of the IDL distribution.

imagen=TVREAD(/jpeg) ;dump into a jpeg file. You can use other format file types such as PNG, TIFF, ...

4.6. Animation

The XINTERANIMATE procedure is a utility for displaying an animated sequence of images.
It requires at least three calls to the routine: one to initialize the animation widget, one to load images, and one to display the images. When initialized using the SET keyword, XINTERANIMATE creates an approximately square pixmap or memory buffer, large enough to contain the requested number of frames of the requested size. Images are loaded using the IMAGE and FRAME keywords.The speed and direction of the display can be adjusted using the widget interface.

MPEG animation files can be created either programmatically using keywords to open and save a file, or interactively using the widget interface. Note that the MPEG standard does not allow movies with odd numbers of pixels to be created. MPEGs files require a license.


IDL> ;please write and execute the following procedure called ''

>pro animacion
>;if s[0] ne 3 then message,'Error: the array must have 3D'
>scale3,xrange=[0,s[1]],yrange=[0,s[2]],zrange=[0,s[3]],ax=0,az=0 ;to be seen from the front
>;initialize xinteranimate
>xinteranimate,set=[512,512,12] ;12 frames varying the angle by 30 degrees (12*30=360)
>;load the frames into xinteranimate
>for i=0,11 do begin
> scale3,az=i*30
> xinteranimate,frame=i,image=polyshade(v,p,/T3D)
>;the T3D keyword is set for the transformations to have effect
>;display the images
>xinteranimate,5 ;the speed can vary from 0 to 100