Sie sind hier: Startseite / Services / Data Analysis and Visualization / Visualization / Software / NCL / examples / source_code / DKRZ NCL legend settings example
Info
Alle Inhalte des Nutzerportal sind nur auf Englisch verfügbar.

DKRZ NCL legend settings example

The script creates 4 plot with different legend settings. 1. using NCL legend defaults, 2. define box size and move it into the plot, 3. change font settings and 4. reverse legend line order and fill legend box with color.

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  meier-fleischer(at)dkrz.de
;----------------------------------------------------------------------
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" ;-- not needed since v6.2.0
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"  ;-- not needed since v6.2.0

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

Artikelaktionen