DKRZ NCL legend settings exampleΒΆ

Example script:

;----------------------------------------------------------------------
;-- DKRZ NCL example:  NCL_legend_settings.ncl
;--
;-- Description:       show legend defaults and resource settings
;--
;--                    1. plot - NCL legend defaults
;--                    2. plot - define legend box size and move it into the plot
;--                    3. plot - change font settings
;--                    4. plot - reverse legend lines and fill legend box with color
;--
;-- 10.07.14  kmf
;----------------------------------------------------------------------

begin
;-- generate dummy data (must be of same dimension size) for the xy-plots
  dims           = 50

  x              =  ispan(0,dims-1,1)                      ;-- x-array from 0 to 60
  x@long_name    = "x variable long_name"                  ;-- define long_name attribute

  data           =  new((/4,dims/),float)
  data(0,:)      =  random_uniform(0.,1600.,dims)          ;-- generate random data values
  data(1,:)      =  random_uniform(0.,1600.,dims)          ;-- generate random data values
  data(2,:)      =  random_uniform(0.,1600.,dims)          ;-- generate random data values
  data(3,:)      =  random_uniform(0.,1600.,dims)          ;-- generate random data values
  data@long_name = "data variable long_name"               ;-- define long_name attribute
  data@units     = "data variable units"                   ;-- define units attribute

;-- open a workstation
  wks_type          = "png"
  wks_type@wkWidth  =  1024
  wks_type@wkHeight =  1024
  wks  = gsn_open_wks(wks_type, "plot_legend_settings")

;-- set resources
  res                        =  True
  res@gsnMaximize            =  True                       ;-- maximize output plot

  res@tiMainString           = "Legend: default"
  res@tiXAxisString          = "velocity in m/s"           ;-- x-axis title
  res@tiYAxisString          = "height in m"               ;-- y-axis title

  res@trYMinF                =  -100                       ;-- y-axis minimum value
  res@trYMaxF                =  2000                       ;-- y-axis maximum value

  res@xyLineColors           =  (/"red","blue","black","green"/) ;-- set the line colors.
  res@xyDashPatterns         =  (/0,1,1,0/)                ;-- line style
  res@xyLineLabelFontColor   =  1                          ;-- black line label color
  res@xyLineThicknessF       =  3.0                        ;-- line thickness for all lines
  res@xyExplicitLegendLabels = (/"u-inst", "u-stat", "v-inst", "v-stat"/)

  res@pmLegendDisplayMode    = "Always"                    ;-- display legend

;-- create 1. plot
  plot  = gsn_xy (wks,x,data,res)

;-- 2. plot define legend box size and move it upwards into the plot
  res@tiMainString           = "Legend: width, height and position"

  res@pmLegendDisplayMode    = "Always"                    ;-- display legend
  res@pmLegendWidthF         =  0.18                       ;-- define legend width
  res@pmLegendHeightF        =  0.11                       ;-- define legend height
  res@pmLegendOrthogonalPosF = -1.10                       ;-- move the legend upward
  res@pmLegendParallelPosF   =  0.21                       ;-- move the legend to the right

;-- create 2. plot
  plot  = gsn_xy (wks,x,data,res)

;-- 3. plot increase legend label font, increase space between legend lines and labels
  res@tiMainString           = "Legend: change font size and view"

  res@xyExplicitLegendLabels = (/"  u-inst", "  u-stat", "  v-inst", "  v-stat"/) ;-- increase space between line and label

  res@pmLegendDisplayMode    = "Always"                    ;-- display legend
  res@pmLegendWidthF         =  0.18                       ;-- define legend width
  res@pmLegendHeightF        =  0.11                       ;-- define legend height
  res@pmLegendOrthogonalPosF = -1.10                       ;-- move the legend upward
  res@pmLegendParallelPosF   =  0.21                       ;-- move the legend to the right

  res@lgAutoManage           =  False                      ;-- switch auto manage off
  res@lgLabelFontHeightF     =  0.022                      ;-- increase label font size

;-- create 3. plot
  plot  = gsn_xy (wks,x,data,res)

;-- 4. plot reverse lines and labels, legend box fill color yellow
  res@tiMainString           = "Legend: reversed and set background color"

  res@xyExplicitLegendLabels = (/"  u-inst", "  u-stat", "  v-inst", "  v-stat"/) ;-- increase space between line and label

  res@pmLegendWidthF         =  0.18                       ;-- define legend width
  res@pmLegendHeightF        =  0.11                       ;-- define legend height
  res@pmLegendOrthogonalPosF = -1.10                       ;-- move the legend upward
  res@pmLegendParallelPosF   =  0.21                       ;-- move the legend to the right

  res@lgAutoManage           =  False                      ;-- switch auto manage off
  res@lgLabelFontHeightF     =  0.022                      ;-- increase label font size
;  res@lgPerimOn              =  True                      ;-- no box around
  res@lgPerimFill            = "SolidFill"                 ;-- fill mode for legend box
  res@lgPerimFillColor       = "yellow"                    ;-- fill color for legend box
  res@lgItemOrder            =  (/3,2,1,0/)                ;-- reverse legend

;-- create 4. plot
  plot  = gsn_xy (wks,x,data,res)

end

Result:

../../../../../../_images/plot_legend_settings.000001_w400.png ../../../../../../_images/plot_legend_settings.000002_w400.png ../../../../../../_images/plot_legend_settings.000003_w400.png ../../../../../../_images/plot_legend_settings.000004_w400.png