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

Giorgio Graffino g.graffino at tim.it
Mon Apr 17 10:27:48 MDT 2023


[UPDATE]
I made some progress using gsnPanelSave adapting replace_tickmark_labels 
and calling it after gsn_panel. To make the individual plots of the size 
I want, I'm drawing some "invisible" labels using res at tmXBLabelFontColor 
= "Background", then I'm redrawing them as red or blue. As you can see, 
the result is not great. 
 
If I set res at tmXBLabelFontColor = "Transparent" the script doesn't work. 
Is there a way to draw transparent labels?  Or is there another way to 
do what I'm trying to do (see email below)?
Cheers,
Giorgio
 
;----------------------------------------------------------------------
; This procedure draw X/Y tickmark labels with custom
; labels created by drawing individual text strings read via 
gsnPanelSave
; (adapted from https://www.ncl.ucar.edu/Applications/Scripts/tm_12.ncl
; and https://www.ncl.ucar.edu/Applications/Scripts/panel_GWLs_Rain.ncl)
;----------------------------------------------------------------------
procedure replace_tickmark_labels_panel(wks,plot)
local txres, xlabels, ylabels, xmin, ymin, xfonth, yfonth, xangle, xjust
begin
;---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
   "tmXBLabelAngleF"         : xangle
   "tmXBLabelJust"           : xjust
 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 plot doesn't overlay with the tickmark labels.
 xmin_ndc = xmin_ndc-0.0075
 ymin_ndc = ymin_ndc-0.0075
 
;---Color the first four labels red
 txres = True
 txres at txJust = xjust
 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 txAngleF = xangle
 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 txAngleF = 0.
 txres at txFontHeightF         = xfont_height
 gsn_text_ndc(wks,ylabels(4:7),xmin_ndc,yvalues_ndc(4:7),txres)
 txres at txAngleF = xangle
 txres at txFontHeightF         = yfont_height
 gsn_text_ndc(wks,xlabels(4:7),xvalues_ndc(4:7),ymin_ndc,txres)
end
 
 
------ Messaggio Originale ------
Da: ncl-talk at mailman.ucar.edu
A: ncl-talk at ucar.edu
Inviato: venerdì 14 aprile 2023 17:10
Oggetto: [ncl-talk] How to use plot tickmark labels with different 
colors
 
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
 
_______________________________________________
ncl-talk mailing list
ncl-talk at mailman.ucar.edu
List instructions, subscriber options, unsubscribe:
https://mailman.ucar.edu/mailman/listinfo/ncl-talk 
<https://mailman.ucar.edu/mailman/listinfo/ncl-talk>
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230417/6a190d53/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HadCM3_plot_maps_cor_8.pdf
Type: application/pdf
Size: 309149 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230417/6a190d53/attachment-0001.pdf>


More information about the ncl-talk mailing list