> Is there a way in NCL to overlay two 2D fields in a contour plot (let's say
> the 1st field in solid lines and the second in dashed lines) and have a
> legend at the top showing 2 items, one for each field?
> All I could get is a legend showing all the line levels of the fields.
> Christian Page
> page AT sca.uqam.ca http://meteocentre.com/
There are a couple of ways you can do this, and I'll give you an
example of one of them. I wrote a little procedure that creates and
draws a legend (gsn_legend_ndc), and added it to gsn_code.ncl, so you
will need to download the latest version of gsn_code.ncl (more on this
in a bit).
The example below is uses data that is all on the same lat/lon grid,
so by using coordinate arrays provided in the netCDF file, it knows
how to do the overlay properly. (This is all done behind the scenes in
the gsn_csm_contour routine.)
This example basically creates two separate contour plots, overlays
one on the other, draws the overlaid plots, and then draws a legend on
If you want to draw the legend *inside* the plot area, then you can do
all of this by using gsn_add_polyline and gsn_add_text. This will then
allow you to panel the plots.
To run the example below, first download the latest gsn_code.ncl
and gsn_csm.ncl from:
Here's the example:
diri = "$NCARG_ROOT/lib/ncarg/data/cdf/" ; directory
fili = "contour.cdf" ; file name
f = addfile (diri+fili , "r") ; add file
T1 = f->T(0,0,:,:) ; get 1st time and level
T5 = f->T(4,0,:,:) ; get 5th time and level
wks = gsn_open_wks ("x11", "legend" ) ; open workstation
res = True
res@vpYF = 0.85
res@gsnDraw = False
res@gsnFrame = False
res@cnLineColor = "Red" ; red contour lines
res@cnLineDashPattern = 0 ; solid lines
plot1 = gsn_csm_contour(wks, T1, res )
res@cnLineColor = "Blue" ; blue contour lines
res@cnLineDashPattern = 12 ; dashed lines
res@cnLineThicknessF = 2.
res@gsnRightString = ""
res@gsnLeftString = ""
res@gsnCenterString = ""
plot5 = gsn_csm_contour(wks, T5, res )
overlay(plot1,plot5) ; Overlay plots; plot1 now
; contains the overlaid plot5.
draw(plot1) ; Draw base plot.
; Set some legend resources.
lgres = True
lgres@lgLineColors = (/"red","blue"/) ; line colors for legend lines
lgres@vpWidthF = 0.8 ; width of legend (NDC)
lgres@vpHeightF = 0.1 ; height of legend (NDC)
lgres@lgPerimOn = False ; turn off perimeter
lgres@lgDashIndexes = (/0,12/) ; dash indexes
lgres@lgLineLabelStrings = (/"",""/) ; no labels in lines
; Draw the legend, indicating the number of items, a label for each
; item, and the X, Y position of the legend in NDC coordinates.
This archive was generated by hypermail 2b29 : Tue Feb 19 2002 - 08:48:13 MST