Re: Color bar positioning problem

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Thu, 7 May 2009 15:01:20 -0600

Hi Oliver,

Actually it turns out that there are LabelBar resources that allow
you to ensure that the corner of the colorbar is positioned precisely
at the justification point. I am enclosing a variation on your test
script that demonstrates how to do it. I will try to explain how it
all works.

By default, with lbAutoManage set to True, the LabelBar code makes
sure that all elements of the LabelBar fit inside the width and
height specified using vpWidthF and vpHeightF. This requires that the
LabelBar controls the font height of the labels. However, depending
on other constraints this often leads to text that is too small. To
achieve control of the font height, you need to turn lbAutoManage
off. Once the user controls the font height, the LabelBar can no
longer ensure that everything will fit inside the specified width and
height, but it still tries to if it can.

A significant resource for your application is lbBoxMinorExtentF.
This specifies the proportion of the space in the direction
perpendicular to the labelbar orientation that is occupied by the
colorbar. By default it is 0.33, or 1/3 of the width specified by
vpWidthF in the case of a vertically oriented labelbar. The total
width is accounted for by 5 elements: the left margin, the colorbar
(box) width, the label offset (which specifies a space between the
colorbar and the labels), the width of the longest text string, and
finally the right margin. If all of these together amount to less
than the width specified by vpWidthF, then the colorbar and the text
are centered within the available width. This is what leads to the
variable distance from the specified position of the top left corner
of the colorbar.

By the way, it is actually not necessary to draw a polyline to trace
the outer boundary of the labelbar. You can simply set the resource
lbPerimOn to True.

To ensure that the top-left corner of the colorbar falls on the
specified point, set the resources as follows:

lbJustification = "topleft"
lbLeftMarginF = 0.0
lbTopMarginF = 0.0 ; eliminate any margin at the top and left of
the colorbar
lbBoxMinorExtentF = 1.0 ; this makes the colorbar width equal to
the full amount of vpWidthF. As a side effect this results in the
labels being drawn entirely outside the labelbar viewport

vpWidthF = .03 ; reduced from 0.1 to maintain approximately the
colorbar width of the original plot.

lbLabelOffsetF = .5 ; this specifies the offset between the colorbar
and the labels as a fraction of vpWidthF (for a vertically oriented
labelbar). Since vpWidthF has become smaller, this value needs to be
increased to maintain approximately the original distance between the
colorbar and the labels.

lbLabelAutoStride = True ; (optional) this ensures that labels do
not overlap when the font height is made very large.

Note that if the text becomes very large the colorbar can still be
pushed down from the justification point because the text extends
above top end of the colorbar. Setting lbLabelAutoStride helps to
prevent this for large text.

If you need to know the ultimate extent of the labelbar after setting
the resources in this manner, you can use NhlGetBB.

Hope this helps.
  -dave
  

On May 5, 2009, at 8:28 AM, Mary Haley wrote:

>
> Oliver,
>
> You ask a good question.
>
> It appears that whatever width and height you specify for the
> labelbar, the labelbar is centered in an invisible boundary created by
> this width and height.
>
> To see this demonstrated, see the attached script. I've drawn a blue
> box around the invisible boundary for each labelbar, using the
> NhlGetBB function to retrieve the coordinates of the boundary.
>
> You can see that the leftmost labelbars are too small to fit the full
> 0.2 width that was specified, so there is a margin between the
> boundary and the leftmost edge of the color bar.
>
> The third labelbar is the closest match to the width specified,
> and hence it's top leftmost corner more closely matches the
> coordinates specified.
>
> I looked through the resources to see if there's anything that can
> control this margin. I thought maybe the lbXXXMarginF resources
> were it, but this doesn't seem to do it either.
>
> I've CC'ed Dave Brown in case he has any ideas on it.
>
> --Mary
>
>
> On Tue, 5 May 2009 Oliver.Fuhrer_at_meteoswiss.ch wrote:
>
>> Dear Mary,
>>
>> Thanks for your help. Your adapted script seems to solve most of the
>> problem. The actual color boxes only start shifting around once the
>> label font size reaches unreasonable size.
>>
>> There's one more thing I don't understand about the behaviour of the
>> color bar. In spite of setting "lbJustification = "TopLeft" the label
>> bar position is not "hooked" to the position I specify (for example
>> (0.2,0.8) for the first label bar in the attached pngs). For a
>> vertical
>> colorbar, I would usually like to position the color boxes exactly
>> with
>> respect to the plot and let the labels shift around corresponding to
>> their size and length. For the moment, the color boxes shift around
>> rather arbitrarily depend on the settings I apply to the colorbar. Is
>> there any way to achieve this behaviour?
>>
>> Thanks for your help and kind regards,
>> Oli
>>
>>
>> ________________________________________
>>
>> Oliver Fuhrer
>> Numerical Models
>>
>> Federal Departement of Home Affairs FDHA
>> Federal Office of Meteorology and Climatology MeteoSwiss
>>
>> Kraehbuehlstrasse 58, P.O. Box 514, CH-8044 Zurich, Switzerland
>>
>> Tel. +41 44 256 93 59
>> Fax +41 44 256 92 78
>> oliver.fuhrer_at_meteoswiss.ch
>> www.meteoswiss.ch - First-hand information
>>
>>
>>
>>> -----Original Message-----
>>> From: Mary Haley [mailto:haley_at_ucar.edu]
>>> Sent: Freitag, 1. Mai 2009 23:01
>>> To: Fuhrer Oliver
>>> Cc: ncl-talk_at_ucar.edu
>>> Subject: Re: Color bar positioning problem
>>>
>>>
>>> Hi Oliver,
>>>
>>> I think it's the lbAutoManage resource that is causing problems
>>> here. I admit that I don't understand the behavior of this resource
>>> very well. It was getting set to False internally, but only
>>> after the labelbar was already created, which was too late.
>>>
>>> I think I've fixed the problem. See the attached "newbar.ncl"
>>> script.
>>>
>>> You'll need to load this after "shea_util.ncl":
>>>
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>> load "./newbar.ncl"
>>>
>>> See what happens when you alternately set r_at_lbAutoManage to True
>>> or False. If you set to False, you should see that your labels
>>> are the actual size expected.
>>>
>>> Thanks for providing a sample script!
>>>
>>> --Mary
>>>
>>> On Fri, 1 May 2009 Oliver.Fuhrer_at_meteoswiss.ch wrote:
>>>
>>>> <<SnagIt1.png>> Hi all!
>>>>
>>>> I'm trying to position a color bar on my plot using NDC
>>> coordinates. The
>>>> color bar seems to shift around rather unexpectedly with
>>> changing the
>>>> label font size (see attached png). Is this a bug or a
>>> feature? If the
>>>> latter, how can I manage to position my color bar exactly in NDC
>>>> coordinates?
>>>>
>>>> Thanks,
>>>> Oli
>>>>
>>>>
>>>> -------- cbarproblem.ncl ------------
>>>>
>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>>> begin
>>>>
>>>> ; define workspace
>>>> wks_type = "ps"
>>>> wks_type_at_wkOrientation = "landscape"
>>>> wks = gsn_open_wks(wks_type, "cbarproblem")
>>>> gsn_define_colormap(wks,"cosam12")
>>>> map = gsn_retrieve_colormap(wks)
>>>>
>>>> ;draw grid
>>>> drawNDCGrid(wks)
>>>>
>>>> ; draw labelbar
>>>> levels = fspan(1.0,12.0,12)
>>>> labels = levels+""
>>>> r = True
>>>> r_at_lbPerimOn = False
>>>> r_at_lbBottomMarginF = 0.0
>>>> r_at_lbTitleOn = False
>>>> r_at_lbTopMarginF = 0.0
>>>> r_at_lbLeftMarginF = 0.0
>>>> r_at_lbRightMarginF = 0.0
>>>> r_at_lbJustification = "TopLeft"
>>>> r_at_lbOrientation = "Vertical"
>>>> r_at_lbAutoManage = False
>>>> r_at_vpWidthF = 0.1
>>>> r_at_lbBoxMinorExtentF = 0.2
>>>> r_at_lbLabelsOn = True
>>>> r_at_lbLabelAlignment = "InteriorEdges"
>>>> r_at_lbLabelJust = "CenterLeft"
>>>> r_at_lbMonoFillPattern = True
>>>> r_at_lbFillColors = map(1:dimsizes(levels),:)
>>>> r_at_vpHeightF = 0.4
>>>> ypos = 0.8
>>>> do i=0,3
>>>> xpos = 0.2 + 0.2*i
>>>> if (i.eq.0) then
>>>> r_at_lbLabelFontHeightF = 0.01
>>>> end if
>>>> if (i.eq.1) then
>>>> r_at_lbLabelFontHeightF = 0.02
>>>> end if
>>>> if (i.eq.2) then
>>>> r_at_lbLabelFontHeightF = 0.05
>>>> end if
>>>> if (i.eq.3) then
>>>> r_at_lbLabelFontHeightF = 0.1
>>>> end if
>>>> gsn_labelbar_ndc(wks,dimsizes(levels)+1,labels,xpos,ypos,r)
>>>> end do
>>>> delete(levels)
>>>> delete(labels)
>>>> delete(r)
>>>> delete(map)
>>>>
>>>> ; finish plot
>>>> frame(wks)
>>>> delete(wks)
>>>>
>>>> end
>>>>
>>>
>>> <cbarproblem.png><labelbar.ncl>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk

Received on Thu May 07 2009 - 15:01:20 MDT

This archive was generated by hypermail 2.2.0 : Mon May 11 2009 - 09:43:46 MDT