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

Anne anne.seidenglanz at unive.it
Sun Jan 13 08:39:03 MST 2019


Hi Adam,

thanks for taking the time to respond to me, and thanks for your
suggestion! In fact I've found a work-around in the meantime that seems to
work quite efficient with what I wanted to achieve, and that is also
similar to your suggestion:

1) I work out my value range and associated number of contour intervals,
depending on how fine I want my contours, using *fspan*:
  ->  e.g.    res at cnLevels               =  fspan(-7, 7, 56) ,     in this
case I decided for a contour spacing of 0.25

2) I use 'span_color_indexes'  and decide on a color palette to retrieve
the desired amount of colours. Here I need to add 2 to the number in
contour levels above:
   -> icol = span_color_indexes("BlWhRe", 58)         so I have 58 color
indexes here (56 contour levels)

3) I insert as many white intervals as I want around the center of my color
range determined by 'icol', here in this example I chose 4 white intervals
that appear in the middle of the color bar:
  ->          icol(27:30)          =  -1

In this way I achieved more or less what I wanted, the only problem I
encountered was that there seemed to be no control over the labal bar
labels anymore, in some plots they appeared rather random, or at least with
a lot of decimal places..

I attach one example of a 2m-temperature plot.

cheers
Anne






On Sat, Jan 12, 2019 at 1:06 AM Adam Phillips <asphilli at ucar.edu> wrote:

> 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>
>

-- 
Nota automatica aggiunta dal sistema di posta.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190113/ff205ca7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2019-01-13 at 16.38.34.png
Type: image/png
Size: 134583 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190113/ff205ca7/attachment-0001.png>


More information about the ncl-talk mailing list