Re: Common legend

From: Mary Haley (haley@XXXXXX)
Date: Tue Dec 11 2001 - 21:50:47 MST


> Hi,
>
> I see that there is a way to add a common labelbar to a contour panel plot.
> (gsnPanelLabelBar)
> Is there a way to add a common legend to a line panel plot?
>
> Thanks,
> Keith

I responded to Keith directly, and forgot to send the answer back to
ncl-talk, so here's a slightly modified version of what I sent him:
 
There's a GSUN example on how to create a legend, but it's tied in
with a particular plot. Since you want to create one that represents
several plots, I would recommend using the object-oriented way to do
it. There are some examples of this at:

   http://ngwww.ucar.edu/ngdoc/ng/qsg/legend/legendoview.html

The legend resources are documented at:

   http://ngwww.ucar.edu/ngdoc/ng/ref/hlu/obj/Legend.obj.html

Basically, you want to turn off frame for gsn_panel (by setting the
resource gsnFrame to False), draw your panel plots (leaving room for
wherever you want the legend), and then create and draw your legend,
wherever you made room for it in the panel.

Here's an example:

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"

begin
;
; Generate some dummy data for three separate plots.
;
  x = ispan(1,5,1)
  y1 = (/1,2,5,4,6/)
  y2 = (/2,1,4,3,6/)
  y3 = (/4,3,5,1,7/)

  wks = gsn_open_wks("x11","plot")

  res = True

  res@gsnDraw = False
  res@gsnFrame = False
;
; Create (but don't draw) three XY plots.
;
  res@xyLineColor = "red"
  xy1 = gsn_xy(wks,x,y1,res)

  res@xyLineColor = "green"
  xy2 = gsn_xy(wks,x,y2,res)

  res@xyLineColor = "blue"
  xy3 = gsn_xy(wks,x,y3,res)
  
;
; Panel these plots.
;
  pnlres = True
  pnlres@gsnFrame = False
  pnlres@gsnPanelBottom = 0.18 ; Leave room for legend at the bottom.

  gsn_panel(wks,(/xy1,xy2,xy3/),(/3,1/),pnlres)
;
; Create a legend object.
;
; Note that there are two kinds of legend labels. Those that are actually
; part of the legend line (uses resources lgLineLabel*), and those that
; go *next* to the legend line (usese resources lgLabel*). Below we are
; turning off the ones that go next to the line.
;
  legend = create "Legend" legendClass wks
    "vpXF" : 0.26
    "vpYF" : 0.17
    "vpWidthF" : 0.5
    "vpHeightF" : 0.15
    "lgPerimOn" : False
    "lgItemCount" : 3
    "lgLineLabelStrings" : (/"y1", "y2", "y3"/)
    "lgLabelsOn" : False
    "lgMonoDashIndex" : True ; Only one dash pattern - solid
    "lgLineColors" : (/"red","green","blue"/)
    "lgMonoLineLabelFontColor" : True ; Only one label color - black
  end create

  draw(legend)
  frame(wks)
end



This archive was generated by hypermail 2b29 : Tue Feb 19 2002 - 09:06:07 MST