;---------------------------------------------------------------------- ; lb_horz_22.ncl ;---------------------------------------------------------------------- ; Concepts illustrated: ; - Attaching a custom labelbar to a bar chart ; - Applying fill opacities to filled elements ; - Using functions for cleaner code ;---------------------------------------------------------------------- ; Population data acquired from ; http://www.worldometers.info/world-population/population-by-country/ ; 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" ;---------------------------------------------------------------------- ; This procedure adds a labelbar to the top right corner of the given ; plot, given a list of labels, fill colors, and an opacity value. ; ; The lbFillOpacityF resource was added in NCL V6.4.0. ;---------------------------------------------------------------------- undef("add_labelbar") procedure add_labelbar(wks,plot,labels,colors,opacity_value) local lbres, vph, vpw, lbid, amres begin getvalues plot ; Get plot size for use in "vpHeightF" : vph ; creating labelbar. "vpWidthF" : vpw end getvalues nboxes = dimsizes(colors) lbres = True ; labelbar only resources lbres@lbAutoManage = True ; Necessary to control sizes lbres@vpWidthF = 0.2 * vpw ; labelbar width lbres@vpHeightF = 0.7 * vph ; labelbar height lbres@lbBoxMajorExtentF = 0.85 ; puts space between color boxes lbres@lbFillColors = colors(::-1,:) ; labelbar colors lbres@lbMonoFillPattern = True ; Solid fill pattern lbres@lbLabelFontHeightF = 0.08 ; font height. default is small lbres@lbLabelJust = "CenterLeft" ; left justify labels lbres@lbLabelAutoStride = False lbres@lbLabelStride = 1 lbres@lbFillOpacityF = opacity_value lbid = gsn_create_labelbar(wks,nboxes,labels(::-1),lbres) ; ; Now, create some annotation resources indicating how we want to ; attach the labelbar to the plot. Here, we are using the top right ; corner of the labelbar as the point which we are going to position ; it, and then we use amParallelPosF and amOrthogonalPosF to indicate ; where we want to place it. ; ; amParallelPosF/amOrthogonalPosF ; ; 0.0/ 0.0 - annotation in dead center of plot ; 0.5/ 0.5 - annotation at bottom right of plot ; 0.5/-0.5 - annotation at top right of plot ; -0.5/-0.5 - annotation at top left of plot ; -0.5/ 0.5 - annotation at bottom left of plot ; amres = True amres@amJust = "TopRight" amres@amParallelPosF = 0.4 amres@amOrthogonalPosF = -0.5 plot@annoid = gsn_add_annotation(plot,lbid,amres) end ;---------------------------------------------------------------------- ; Main code ;---------------------------------------------------------------------- begin ;---Generate list of 30 countries with the highest populations. countries = (/"China","India","U.S.","Indonesia","Brazil","Pakistan",\ "Nigeria","Bangladesh","Russia","Mexico","Japan",\ "Ethiopia", "Philippines","Egypt","Viet Nam","DR Congo",\ "Germany", "Iran", "Turkey","Thailand","U.K.","France",\ "Italy","Tanzania","South Africa","Myanmar","South Korea",\ "Kenya","Colombia","Spain"/) population = (/1415045928,1354051854,326766748,266794980,210867954,\ 200813818, 195875237,166368149,143964709,130759074,\ 127185332, 107534882,106512074, 99375741, 96491146,\ 84004989, 82293457, 82011735, 81916871, 69183173,\ 66573504, 65233271, 59290969, 59091392, 57398421,\ 53855735, 51164435, 50950879, 49464683, 46397452/) ncountries = dimsizes(countries) x = ispan(1,ncountries,1) rgba = span_color_rgba("matlab_jet",ncountries) ;---Start the graphics wks = gsn_open_wks("png","lb_horz") ; send graphics to PNG file res = True ; plot mods desired res@gsnMaximize = True ; maximize plot in frame res@gsnDraw = False ; don't draw plot yet res@gsnFrame = False ; don't advance frame yet res@trXMinF = 0 ; bring bars down to zero res@trYMinF = min(x) - 1 ; adds space on either end res@trYMaxF = max(x) + 1 ; of the 1st and last bars res@gsnXYBarChart = True ; turn on bar chart res@gsnXYBarChartBarWidth = 0.75 ; change bar widths res@gsnXYBarChartColors = rgba res@gsnXRefLine = 0 res@tmYLOn = False ; turn off X tickmarks and labels ;---Labels for X axis res@tmXBMode = "Explicit" res@tmXBValues = ispan(0,14,2)*1e8 ; res@tmXBLabels = (/"0","200 million","400 million","600 million","800 million",\ ; "1 billion","1.2 billion", "1.4 billion"/) res@tmXBLabels = (/"0", "200M", "400M", "600M", "800M", "1BN", \ "1.2BN", "1.4BN"/) res@tiMainString = "Top " + ncountries + " most populous countries in 2018" ;---Create first plot; no opacities applied res@tiXAxisString = "No opacity applied" plot = gsn_csm_xy (wks,population,x,res) ; Create plot, but don't draw it yet. add_labelbar(wks,plot,countries,rgba,1.0) ; Attach a custom labelbar to the plot draw(plot) ; Draws plot and labelbar frame(wks) ;---Create second plot; opacity applied to labelbar colors only res@tiXAxisString = "Opacity applied to labelbar only" plot = gsn_csm_xy (wks,population,x,res) add_labelbar(wks,plot,countries,rgba,0.50) draw(plot) frame(wks) ;---Create third plot; multiple opacities applied to plot and labelbar rgba(:,3) = fspan(1,0.01,ncountries) res@gsnXYBarChartColors = rgba res@tiXAxisString = "Opacity applied to bars and labelbar" plot = gsn_csm_xy (wks,population,x,res) add_labelbar(wks,plot,countries,rgba,1.0) draw(plot) frame(wks) end