[ncl-talk] Forcing white in the middle of a symmetric colorbar

Adam Phillips asphilli at ucar.edu
Fri Jan 11 17:06:14 MST 2019


HI Anne,
If you want a symmetric colorbar with white in the middle, you want to use
(the default) gsnSpreadColors, you do not want to always set cnLevels, and
you do not want to specify what colors you use by setting cnFillColors (for
every color), then I think the best thing to do would be to use getvalues
to retrieve the colors after the plot has been created, modify them, and
then use setvalues to set them. The following should get you started. Note
that the label bar has to be created after gsn_csm_contour_map is called as
you are modifying the colors after the plot is drawn.

If you have any further questions let ncl-talk know.
Adam

begin
;************************************************
; read in netCDF file
;************************************************
  a = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/uv300.nc","r")
  u = a->U(1,:,:)                                ; read July zonal winds
;************************************************
; create plot
;************************************************
  wks = gsn_open_wks("png","color")             ; send graphics to PNG file
  gsn_define_colormap(wks,"ncl_default")
  cmap = read_colormap_file("ncl_default")

  res                      = True               ; plot mods desired
  res at tiMainString         = "Default Color"    ; main title
  res at cnFillOn             = True               ; turn on color fill
  res at gsnDraw = False
  res at gsnFrame = False
  res at lbLabelBarOn = False

  plot = gsn_csm_contour_map(wks,u, res)     ; create plot

  getvalues plot at contour    ; retrieve colors/levels from plot
    "cnFillColors" : colors
    "cnLevels"     : levels
  end getvalues

  ncolors = dimsizes(colors)
  colors(ncolors/2-1) = 0    ; set two middle colors to white
  colors(ncolors/2) = 0       ; if you have odd number of colors this
should be modified

  print(colors)

  setvalues plot at contour
     "cnFillColors" : colors       ; reset the colors
  end setvalues

  lbres                    = True
  lbres at vpWidthF           = 0.55
  lbres at vpHeightF          = 0.075

  lbres at lbPerimOn          = False            ; Turn off perimeter.
  lbres at lbOrientation      = "Horizontal"     ; Default is vertical.
  lbres at lbFillColors       = colors
  lbres at lbMonoFillPattern  = True             ; Fill them all solid.
  lbres at lbLabelFontHeightF = 0.020            ; Label font height
  lbres at lbLabelAlignment   = "InteriorEdges"
  lbid = gsn_create_labelbar(wks,dimsizes(levels),levels,lbres)       ;
create the labelbar


  amres                  = True
  amres at amJust           = "BottomCenter"
  amres at amParallelPosF   =  0.0
  amres at amOrthogonalPosF = 0.75
  annoid = gsn_add_annotation(plot,lbid,amres)    ; attach the labelbar to
the plot
  draw(plot)
  frame(wks)
end





On Fri, Jan 11, 2019 at 4:36 AM Anne SEIDENGLANZ <anne.seidenglanz at unive.it>
wrote:

> Hello,
>
> I am trying to combine the possibilities in NCL to
>
> 1) use the a symmetric colorbar using the gsnSpreadColorStart / End
> resources (and res at cnLevelSelectionMode = “ManualLevels”)
>
> and
>
> 2) forcing white for 1-2 contours in the middle for things like anomaly
> fields.
>
> Since I need a very fine scale, and therefore a lot of contours, setting
> them explicitely using res at cnExplicitLevels is not very feasible for me,
> and I’m trying to avoid to set these levels each single time.
>
> So my question is:  Is there a way to use gsnSpreadColorStart and
> gsnSpreadColorEnd so as to have a symmetric color bar  AND  having 1-2
> contours of white in the middle?
> I know there are some white-in-the-middle pre-defined color maps in NCL
> but some of them don’t have a fine enough color scale.
>
> Here’s an example of I have been trying to attempt:
>
>  res at cnLevelSelectionMode      = "ManualLevels"
>  res at cnMinLevelValF         =  -5
>  res at cnMaxLevelValF         =  5
>  res at cnLevelSpacingF        =  0.25
>
>  res at gsnSpreadColors   =  True
>  res at gsnSpreadColorStart   =  2
>  res at gsnSpreadColorEnd     =  255
>
>  res at cnLevelSelectionMode    := "ExplicitLevels"
>  res at cnLevels                =  fspan(-5, 5, 0.25)
>  res at cnFillColors(20:22)        =  1
>
>
> so I tried to overwrite the SelectionMode resource to ‘Explicit’  and then
> picking trying to pick only the center conoturs for white.. but obviously
> it didn’t work in this way.
>
> I remember there is some function that does exactly take care of this but
> I do not remember which one it was.
>
>
> Any help is appreciated!
> Thanks,
> Anne
>
>
>
>
> On 3 Jan 2019, at 18:55, Adam Phillips <asphilli at ucar.edu> wrote:
>
> Hi David,
> There's a function (add_map_tickmarks2) that you can use embedded in some
> of the NCL examples that will add tick mark labels to rectangular
> projections. I took mptick example #9 here:
> http://www.ncl.ucar.edu/Applications/mptick.shtml#ex9
> and modified it to (mostly) match your projection. The script can be run
> on its' own and runs quickly, making trial and error less painful. I have
> attached the modified script and the resulting graphic.
> Hope that helps!
> Adam
>
> On Wed, Jan 2, 2019 at 3:08 PM David Hebert, Code 7322 <
> david.hebert at nrlssc.navy.mil> wrote:
>
>> I am attempting to make a regional plot using stereographic projection,
>> and limit the projection using ‘corners’. I am getting only  longitude
>> labels along the bottom and left plot boundary. See attached plot.
>>
>>
>>
>> The relevant settings I am using in my script are:
>>
>>
>>
>>   res at mpProjection          = "Stereographic"
>>
>>   res at gsnAddCyclic          = False
>>
>>   res at mpGridAndLimbOn       = True              ; turn on lat/lon lines
>>
>>   res at mpPerimOn             = False             ; turn off box around
>> plot
>>
>>   res at pmTickMarkDisplayMode = "Always"      ; turn on ticks
>>
>>
>>
>>   res at tmYROn = False
>>
>>   res at tmXTOn = False
>>
>>
>>
>>   res at mpGridLonSpacingF = 15.
>>
>>   res at mpGridLatSpacingF = 2.
>>
>>
>>
>>   ; set map limits based on tlat,tlon
>>
>>     res at mpLimitMode        = "Corners"
>>
>>     res at mpLeftCornerLatF   = tlat(0,0)
>>
>>     res at mpLeftCornerLonF   = tlon(0,0)
>>
>>     res at mpRightCornerLatF  = tlat(nj2-1,ni2-1)
>>
>>     res at mpRightCornerLonF  = tlon(nj2-1,ni2-1)
>>
>>     res at mpRelativeCenterLon         = True      ; set a center lon
>>
>>     res at mpCenterLonF                = 180.0
>>
>>     res at mpRelativeCenterLat         = True       ; set a center lat
>>
>>     res at mpCenterLatF                = -90.0           ; center lat
>>
>>
>>
>> Any suggestions on how to get the latitude labels on the left axis? Thank
>> you for your help!
>>
>>
>>
>>
>>
>> *David A. Hebert*
>>
>> Oceanography Division, Code 7322
>> Naval Research Laboratory
>> Stennis Space Center, MS 39529
>> (228)688-5846; DSN 828-5846
>> http://www.nrl.navy.mil
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>
>
> --
> Adam Phillips
> Associate Scientist,  Climate and Global Dynamics Laboratory, NCAR
> www.cgd.ucar.edu/staff/asphilli/   303-497-1726
>
> <http://www.cgd.ucar.edu/staff/asphilli>
> <tickmarks.ncl><mptick.png>_______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
>
> Nota automatica aggiunta dal sistema di posta.
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>


-- 
Adam Phillips
Associate Scientist,  Climate and Global Dynamics Laboratory, NCAR
www.cgd.ucar.edu/staff/asphilli/   303-497-1726

<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190111/d1aa21cd/attachment.html>


More information about the ncl-talk mailing list