;******************************************************* ; leg_10.ncl ; ; Concepts illustrated: ; - Drawing a legend inside an XY plot ; - Masking the XY curves behind a legend ; - Drawing a legend on top of everything in an XY plot ; - Drawing vertical grid lines in an XY plot ; - Changing the width and height of a legend ; - Changing the legend box fill color ; - Adding labels to curves in an XY plot ; ;******************************************************* ; ; 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" begin ; ; Define the number of points in each curve. ; NPLOTS = 4 NCURVES = 4 NPTS = 50 PI100 = 0.031415926535898 ; ; Create data for the four XY plots. ; y = new((/NPLOTS,NCURVES,NPTS/),float) theta = PI100*ispan(0,NPTS-1,1) y(0,0,:) = sin(theta) y(0,1,:) = 2+sin(2*sqrt(fabs(theta))) ; Make sure they y(0,2,:) = 4+sin(3*sqrt(fabs(theta))) ; don't intersect. y(0,3,:) = 6+sin(10*sqrt(fabs(theta))) y(1,0,:) = cos(theta) y(1,1,:) = 2+cos(2*sqrt(fabs(theta))) y(1,2,:) = 4+cos(4*sqrt(fabs(theta))) y(1,3,:) = 6+cos(8*sqrt(fabs(theta))) y(2,0,:) = 3+sin(theta) y(2,1,:) = 5+sin(1.5*sqrt(fabs(theta))) y(2,2,:) = 4+sin(6*sqrt(fabs(theta))) y(2,3,:) = 5+sin(2.5*sqrt(fabs(theta))) y(3,0,:) = 3+cos(theta) y(3,1,:) = 5+cos(2*sqrt(fabs(theta))) y(3,2,:) = 4+cos(2.5*sqrt(fabs(theta))) y(3,3,:) = 5+cos(8*sqrt(fabs(theta))) wks = gsn_open_wks("png","leg_panel") ; send graphics to PNG file res = True ; plot mods desired res@gsnDraw = False ; Don't draw plot, will overlay later res@gsnFrame = False ; Don't advance frame res@gsnMaximize = True res@pmLegendDisplayMode = "Always" ; Turn on a legend res@pmLegendOrthogonalPosF = -0.35 ; Move legend inside plot res@pmLegendParallelPosF = 0.8 ; Move legend to right res@pmLegendWidthF = 0.13 ; Change width and height res@pmLegendHeightF = 0.1 res@xyLabelMode = "Custom" res@xyExplicitLabels = (/"w","x","y","z"/) ; explicit labels res@xyLineLabelFontHeightF = 0.015 ; font height res@lgPerimFill = "SolidFill" ; Fill legend box w/white res@lgPerimFillColor = "yellow" ; Just for fun, change the color ; ; To get the legend to draw on top of grid lines, first create the ; plot with the grid lines and no legend, and then create the plot ; again with no grid lines but the legend turned on. Overlay this ; second plot on the first one using "overlay". ; ;---Set resources specific to base plots res@tmXMajorGrid = True ; Turn on vertical grid lines. The ; legend will be under the grid lines. res@pmLegendDisplayMode = "Never" ; Turn off legend for base plots base_plot1 = gsn_csm_y(wks,y(0,:,:),res) ; Draw plot with grid lines, but no legend base_plot2 = gsn_csm_y(wks,y(1,:,:),res) ; Draw plot with grid lines, but no legend base_plot3 = gsn_csm_y(wks,y(2,:,:),res) ; Draw plot with grid lines, but no legend base_plot4 = gsn_csm_y(wks,y(3,:,:),res) ; Draw plot with grid lines, but no legend ;---Set resources specific to overlay plots res@pmLegendDisplayMode = "Always" ; Turn legend back on res@tmXMajorGrid = False ; Don't draw grid lines 2nd time ovrly_plot1 = gsn_csm_y(wks,y(0,:,:),res) ; Draw plot again without grid ovrly_plot2 = gsn_csm_y(wks,y(1,:,:),res) ; Draw plot again without grid ovrly_plot3 = gsn_csm_y(wks,y(2,:,:),res) ; Draw plot again without grid ovrly_plot4 = gsn_csm_y(wks,y(3,:,:),res) ; Draw plot again without grid ; ; The overlay call literally overlays the "ovrly" plots on top of the ; "base" plots, and makes them part of the base plots. When you draw ; the base plots in the gsn_panel call, the overlaid plots will get ; drawn too. ; overlay(base_plot1,ovrly_plot1) overlay(base_plot2,ovrly_plot2) overlay(base_plot3,ovrly_plot3) overlay(base_plot4,ovrly_plot4) ;---Panel all four plots. pres = True pres@gsnMaximize = True pres@txString = "Legend drawn on top of grid lines" gsn_panel(wks,(/base_plot1,base_plot2,base_plot3,base_plot4/),(/2,2/),pres) end