;************************************************* ; leg_sl_5.ncl ; ; Concepts illustrated: ; - Manually creating a legend using simple_legend ; - Changing the line colors of lines inside a legend ; - Changing the dash patterns of lines inside a legend ; - Customizing the labels in a legend ; ;************************************************ ; ; These files are loaded by default in NCL V6.2.0 and newer ; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" ; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" ; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl" ;************************************************ begin ;************************************************ ; read in netCDF file ;************************************************ a = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/uv300.nc","r") ;************************************************ ; read in zonal winds ;************************************************ lat=a->lat nlat=dimsizes(lat) u = a->U(0,:,:) uz1 = dim_avg(u) u = a->U(1,:,:) uz2 = dim_avg(u) uz1!0 = "lat" uz1&lat = lat uz2!0 = "lat" uz2&lat = lat v = a->V(0,:,:) vz1 = dim_avg(v) v = a->V(1,:,:) vz2 = dim_avg(v) vz1!0 = "lat" vz1&lat = lat vz2!0 = "lat" vz2&lat = lat ;************************************************ ; Combine uz2 and vz2 into one array ;************************************************ data_1 = new( (/2,nlat/),float) data_1(0,:)=uz1 data_1(1,:)=vz1 data_2 = new( (/2,nlat/),float) data_2(0,:)=uz2 data_2(1,:)=vz2 ;************************************************ ; create plot ;************************************************ wks = gsn_open_wks("png","leg_sl") ; send graphics to PNG file plot = new(2,graphic) res = True res@gsnDraw = False res@gsnFrame = False res@xyMarkLineModes = (/"Lines"/) ; line style res@xyLineThicknesses = (/2.,2.,2.,2./) ; line thickness res@xyLineColors = (/"foreground","green"/) ; choose line colors plot(0) = gsn_csm_xy(wks,lat,data_1,res) res@xyLineColors = (/"blue","red"/) ; change line colors plot(1) = gsn_csm_xy(wks,lat,data_2,res) ;************************************************ ; Set resources for customizing a simple legend ;************************************************ genres = True genres@XPosPercent = 80 ; move to the right genres@ItemSpacePercent = 6 textres = True textres@lgLabelFontHeights = 0.025 textres@lgLabels = (/"u1", "v1"/) textres@lgPerimOn = False ; no perimeter textres@lgItemCount = 2 ; how many lineres = True lineres@lgLineThicknesses = 2.5 ; line thickness lineres@LineLengthPercent = 8 ; expressed as %, 0->100, length of line lineres@lgLineLabelFontHeights = 0.015 ; font height lineres@lgDashIndexes = (/0,1,0,1/) ; line patterns lineres@lgLineColors = (/"foreground","green"/) dum = simple_legend(wks, plot(0), genres, lineres, textres) textres@lgLabels = (/"u2", "v2"/) lineres@lgLineColors = (/"blue", "red"/) ;************************************************ ; Draw the simple legend before paneling the two plots ;************************************************ dum = simple_legend(wks, plot(1), genres, lineres, textres) ;************************************************ ; panel resources and plot ;************************************************ pnlres = True pnlres@gsnPanelMainString = "Using simple_legend" gsn_panel(wks,plot,(/2,1/),pnlres) end