[ncl-talk] How to use plot tickmark labels with different colors

Giorgio Graffino g.graffino at tim.it
Fri Apr 14 10:10:09 MDT 2023


Hello NCLers,
 
I need to draw tickmark labels with two different colors (red and blue). 
The script is quite complex; it uses part of 
https://www.ncl.ucar.edu/Applications/Scripts/table_8.ncl to draw many 
table plots, which become part of a panel plot (see attached 
HadCM3_plot_maps_cor_3.png). I can't find a resource like 
tmXBLabelFontColors, or something able to handle an array of colors like 
xyLineColors does for lines. Does anyone know if such resource exist?
 
I tried to generate colored labels in another way. I found this example 
(https://www.ncl.ucar.edu/Applications/Scripts/tm_12.ncl), containing a 
procedure able to remove labels from a plot and redraw them. My version 
of the procedure is pasted below; it reads the value and the location of 
the labels from the plot function, and draws them again using 
gsn_text_ndc. I included the procedure in my script but the result is 
not the intended one (see attached HadCM3_plot_maps_cor_3colored.png).
 
I found out that there is an unadvertised gsnPanelSave resource used in 
https://www.ncl.ucar.edu/Applications/Scripts/panel_GWLs_Rain.ncl to to 
keep the plots in their resized state. I think it might help. Can anyone 
please tell me how it works?
 
Cheers,
Giorgio
 
;----------------------------------------------------------------------
; This procedure replaces a plot's X/Y tickmark labels with custom
; labels created by drawing individual text strings 
; (adapted from https://www.ncl.ucar.edu/Applications/Scripts/tm_12.ncl)
;----------------------------------------------------------------------
procedure replace_tickmark_labels(wks,plot)
local txres, xlabels, ylabels, xmin, ymin, xfonth, yfonth
begin
;---Turn off plot's X and Y tickmark labels
 setvalues plot
   "tmXBLabelsOn"  : False
   "tmYLLabelsOn"  : False
 end setvalues
   
;---Retrieve some values so we can reconstruct the labels
 getvalues plot
   "tmXBLabels"              : xlabels
   "tmYLLabels"              : ylabels
   "tmXBValues"              : xvalues
   "tmYLValues"              : yvalues
   "trXMinF"                 : xmin
   "trYMinF"                 : ymin
   "tmYLMajorOutwardLengthF" : ylength
   "tmXBMajorOutwardLengthF" : xlength
   "tmXBLabelFontHeightF"    : xfont_height
   "tmYLLabelFontHeightF"    : yfont_height
 end getvalues
;---Get the NDC locations for the new tickmark labels
 xvalues_ndc = new(dimsizes(xvalues),float)
 yvalues_ndc = new(dimsizes(yvalues),float)
 xmin_ndc    = new(1,float)
 ymin_ndc    = new(1,float)
 datatondc(plot,xvalues,yvalues,xvalues_ndc,yvalues_ndc)
 datatondc(plot,xmin,ymin,xmin_ndc,ymin_ndc)
;---Make sure the tickmark labels don't touch the tickmarks.
 xmin_ndc = xmin_ndc-(1.8*ylength)     
 ymin_ndc = ymin_ndc-(1.8*xlength)
 
;---Color the first four labels red
 txres = True
 txres at txFontColor = "Red"
;---Draw the first four labels
 txres at txFontHeightF         = xfont_height
 gsn_text_ndc(wks,ylabels(0:3),xmin_ndc,yvalues_ndc(0:3),txres)
 txres at txFontHeightF         = yfont_height
 gsn_text_ndc(wks,xlabels(0:3),xvalues_ndc(0:3),ymin_ndc,txres)
;---Color the last four labels blue
 txres at txFontColor = "Blue"
;---Draw the last four labels
 txres at txFontHeightF         = xfont_height
 gsn_text_ndc(wks,ylabels(4:7),xmin_ndc,yvalues_ndc(4:7),txres)
 txres at txFontHeightF         = yfont_height
 gsn_text_ndc(wks,xlabels(4:7),xvalues_ndc(4:7),ymin_ndc,txres)
end
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230414/8d4f5d97/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HadCM3_plot_maps_cor_3colored.png
Type: image/png
Size: 163403 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230414/8d4f5d97/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HadCM3_plot_maps_cor_3.png
Type: image/png
Size: 197811 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230414/8d4f5d97/attachment-0003.png>


More information about the ncl-talk mailing list