NCL: A Tale of Two Titles

When I’m finalizing figures I like to go through and make sure they are adequately annotated. For example, it’s nice to have the units and variable name[s] mentioned somewhere on the plot for the casual reader that’s just skimming through and doesn’t want to take the time read through the figure captions. However, lately I’ve been running into the issue of NCL incorrectly positioning the titles of the x-axis and the colorbar (see example below).

This has really been driving me crazy lately because it’s not easy to fix.

The figure above can be generated easily with the following code:

begin
 fig_file = "test.axis_string"
 fig_type = "png"
 pi = 3.14159
 nx = 100
 ;------------------------------------------------------------
 ;------------------------------------------------------------
 xx = tofloat(ispan(0,nx-1,1))
 x_func = sin(xx/(nx-1)*pi*2.)
 y_func = sin(xx/(nx-1)*pi)
 X = new((/nx,nx/),float)
 X = conform(X,y_func,0) * conform(X,x_func,1)
 ;------------------------------------------------------------
 ;------------------------------------------------------------
 wks = gsn_open_wks(fig_type,fig_file)
  res = True
  res@vpHeightF = 0.3
  res@cnFillOn = True
  res@lbTitlePosition = "Bottom"
  res@lbTitleString = "Label Bar Title"
  res@tiXAxisString = "X-Axis Title"
  res@tiXAxisFontHeightF = 0.02

 plot = gsn_csm_contour(wks,X,res)
 ;------------------------------------------------------------
 ;------------------------------------------------------------
 if fig_type.eq."png" then 
 print("")
 print(" "+fig_file+".png") 
 print("")
 end if
 ;------------------------------------------------------------
 ;------------------------------------------------------------
end

The only way I’ve found to fix the problem is manually tuning seperate upward and downward shifts of each title. For example, I can remedy the plot above by adding the following plot resources:

; Move X-axis title upward
 res@tiXAxisOffsetYF = 0.12

 ; Move Labelbar downard
 shift = 0.25
 res@lbTopMarginF    = shift
 res@lbBottomMarginF = -shift

The improved figure is shown below.

I wish NCL didn’t do this, because each figure needs to be “re-tuned” to make sure there’s no overlap. In some cases, shifting the titles causes them to go “off the page” (i.e. out of the view port), which then has to be fix by changing the view port resources (e.g. res@vpHeightF), which forces you to re-tune the shift parameters.

Hopefully this annoying issue will be fixed in a future NCL release.

Leave a Reply

Your email address will not be published. Required fields are marked *