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

Adam Phillips asphilli at ucar.edu
Tue Apr 18 17:12:38 MDT 2023


Hi Giorgio,
You could try setting tmXBLabelFontColor to a RGBA value, setting the A
value to 0. Using xy example #1 as a template:

begin
 f     = addfile ("$NCARG_ROOT/lib/ncarg/data/cdf/uv300.nc","r")
 u     = f->U                                    ; get u data

 wks   = gsn_open_wks ("png","xy")               ; send graphics to PNG file
 res                  = True                     ; plot mods desired
 res at gsnFrame = False
 res at tiMainString     = "Basic XY plot"          ; add title
 res at tmXBLabelFontColor = (/0,0,1,0./).   ; <------ Set to blue, but make
the color completely transparent.
 plot  = gsn_csm_xy (wks,u&lat,u(0,:,{82}),res) ; create plot
 res at tmXBLabelFontColor = (/0,1,.05,1./).   ; <---- Set to green, but make
the color opaque.
 plot2  = gsn_csm_xy (wks,u&lat,u(0,:,{82}),res) ; create plot
 frame(wks)
end

If you run the above script you will see clean green X-axis labels (without
any weird blue outlines to the green labels).

Hope that helps!
Adam


On Mon, Apr 17, 2023 at 10:27 AM Giorgio Graffino via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> [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
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at mailman.ucar.edu
> List instructions, subscriber options, unsubscribe:
> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>


-- 
Adam Phillips
Associate Scientist IV, Climate Analysis Section
Climate and Global Dynamics Laboratory
National Center for Atmospheric Research
www.cgd.ucar.edu/staff/asphilli/

<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230418/e6a381ca/attachment.htm>


More information about the ncl-talk mailing list