How to Overlay a Contour Plot Over a 2D Plot

Here is code that produces a graph: 

XT = DblArr(1000)
YT = DblArr(1000)
XMIN = -3.0
XMAX = 3.0
YMIN = -3.0
YMAX = 3.0
M = 40
N = 40
W = DblArr(M,N)

OpenR, lun, 'NPOINT', /Get_Lun
Point_Lun, lun, 0
ReadF, lun, npt
npoint = FIX(npt)
Free_Lun, lun

OpenR, lun, 'TRAJ', /Get_Lun
Point_Lun, lun, 0
array = DblArr(2, npoint)
ReadF, lun, array
XT = array(0,*)
YT = array(1,*)
Free_Lun, lun

FOR j=0,(N-1) DO BEGIN
   Y=(YMAX-YMIN)*(FLOAT(j)/FLOAT(N-1))+YMIN
   FOR i=0,(M-1) DO BEGIN
     X = 5. * i/(M-1) - 0.5
     X=(XMAX-XMIN)*(FLOAT(i)/FLOAT(M-1))+XMIN
     W[i,j] = (X*Y)^2*EXP(-(X^2+Y^2))
  ENDFOR
ENDFOR

Plot, XT, YT, XTITLE = 'X', YTITLE = 'Y', $
      XSTYLE = 1, XRANGE = [XMIN,YMAX], $
      YSTYLE = 1, YRANGE = [YMIN,YMAX]

END
 



If instead of the plot command, the contour command is issued as follows, here is the result:

Contour, W, NLevels = 5

Note:  the X and Y axes are integers from 0 to 39 which constitute the subscripts for the two dimensions of the array.
 
 
 
 
 
 



To get the contour plot to overlie the graph, you must do two things.:  1) add /noerase to the contour command*, and 2) change the axes for the contour as shown in the code.  Note that you must use FIndGen because you are actually modifying the subscripts.  You may have to experiment a little to get the values to come out right in your own code.

*You must use "noerase" instead of "overplot."  Overplot is confused because you're going from 2D to 3D

Plot, XT, YT, XTITLE = 'X', YTITLE = 'Y', $
      XSTYLE = 1, XRANGE = [XMIN,YMAX], $
      YSTYLE = 1, YRANGE = [YMIN,YMAX]

xax = FIndGen(40) * (6./39) - 3
yax = FIndGen(40) * (6./39) -3

Contour, W, xax, yax, XStyle=1,YStyle=1, $
    NLevels = 5, /noerase