[GRASS-user] Horizontal legend font size

Paul Moen pmoen at nd.gov
Wed Aug 13 09:50:08 EDT 2008

What I am doing is creating a map dynamically for our web.  The grass part
of the script is,
            g.mapset mapset=Temp 2>>$errtext
            v.in.ascii input=$textFile output=$RasName fs='tab' x=3 y=4
skip=0 cat=1 2>>$errtext
            v.surf.rst input=$RasName at Temp layer=1 zcolumn=dbl_3 tension=40.
elev=$RasName2 dmax=5 dmin=2 2>>$errtext
            g.remove vect=$RasName 2>>$errtext
            r.colors map=$RasName2 at Temp color=arb-precip 2>>$errtext
            r.out.tiff input=$RasName2 at Temp compression=lzw -t
output=\"".$workingdir."/".$RasName."\" 2>>$errtext
            d.mon start=PNG 2>>$errtext
            d.frame -c frame=map at=11,100,0,100
            d.rast $RasName2 at Temp 2>>$errtext
            d.vect counties at Temp fcolor=none type=boundary 2>>$errtext
            d.frame -c frame=leg at=1,10,5,95 2>>$errtext
            d.legend -s range=0,15.0 labelnum=5 at=70,99,7,93
map=$RasName2 at Temp  2>>$errtext
            d.mon stop=PNG 2>>$errtext
            g.remove rast=$RasName2 2>>$errtext

On 8/13/08 2:42 AM, "Hamish" <hamish_b at yahoo.com> wrote:

> Paul Moen wrote:
>> I want to create a horizontal legend for a raster.
>> The docs say,
>>> When using the mouse or at to size & place the
>>   legend, a user may create a horizontal legend by making
>>   the box wider than it is tall.
>> I read the previous line to mean the only way to create a
>> legend is to use at and make the box wider than it is tall.
>>  Is that correct?
> Yes.
>> Can I make a legend horizontal without using at=bottom,top,left,right
> From the command line and xmons you can use 'd.legend -m' to place the
> legend with the mouse. I am not sure if you get the same chance from
> the GUIs or if the only option there is filling in at=.
>> The docs also say,
>>> Vertical legends produced with d.legend will place text
>>   labels to the right of the legend box, horizontal legends
>>   will place text below. This text will be auto-scaled to fit
>>   within the frame, reducing the size of the legend if
>>   necessary. Legends positioned with the mouse or with
>>   the at option will not auto-scale text, in order to provide
>>   more control to the user.
>> The last line that says that the text will not auto-scale
>> when using at,
> correct. Only d.legend without at= or -m will autoscale. If manually
> placed it will assume you know what you are doing and give you exactly
> what you requested.
> WRT horizontal scales, I think it only autoscales from the right side of the
> window, not the bottom. But anyway, full labels are useless in a
> horiz legend, they just fall on top of each other.
> but see below- what is being autoscaled is the category label.
> e.g. "d.legend -n landcover.30m" in the spearfish dataset then adjust
> the window so it is narrower and watch it autoscale.
>> which means that a horizontal legend¹s text will never auto-scale
>> since the horizontal legend can only be made by using at to create
>> a box that is wider than it is tall. Am I misinterpreting anything?
> a couple of things:
> you can make a box wider than it is tall on an xmon using the -m "place
> with mouse" command line flag. The GUIs don't use xmons, which forces the
> use of at=.
> But what I meant by "text labels" is for categorical legends not tick numbers.
> e.g.:
>  [red] pine forest
>  [blue] deciduous forest
>  [grey] urban area
> in this case the "pine forest" text would automatically shrink if it would
> fall off the right side of the window.
> For horizontal legends font size is purely a function of legend width.
> (for vertical legends it is the legend height)
> I guess for horiz legends I could look at rotating the font 90deg for
> categorical legends with text labels (set with r.cats/r.category). But
> I'd wait for strong user demand before spending any time on that.
>> My problem is that I get a horizontal legend that looks great except
>> the text is huge and looks awful.
> you mean numbers (category or value) or text labels of categories?
> could you provide a screenshot?
I have no categories, so it would be the values.  You can see the output at
I would like to add a few more labels labelnum=7 with smaller text.

It would also be nice to turn off the borders on the frames, although that
is another topic.
> is this a floating point (FCELL or DCELL) or categorical (CELL) map?
>  [r.info]
>> How can I change the size of the text in the legend?
> you need to recompile d.legend with the following patch:
> Index: display/d.legend/main.c
> ===================================================================
> --- display/d.legend/main.c     (revision 32734)
> +++ display/d.legend/main.c     (working copy)
> @@ -672,7 +672,7 @@
>             if (!horiz)
>                 txsiz = (int)((y1 - y0) / 20);
>             else
> -               txsiz = (int)((x1 - x0) / 20);
> +               txsiz = (int)((x1 - x0) / 25);
>             /* scale text to fit in window if position not manually set */
>             /* usually not needed, except when frame is really narrow   */
> adjust the divisor to suit. The above patch makes the font super tiny
> for legends which are around 2-3 inches wide, so I am loathe to apply
> it in SVN. Maybe it would be better to make textsize it a fn of width^2
> so the rate of change was not so dramatic.
I will see if I can figure out the recompiling to see how it works.
>> The following are my commands.
>> d.rast raster at Temp
>> d.frame -c frame=leg at=1,10,5,95
>> d.legend -s range=0,13.5 labelnum=5 at=80,100,5,95 map=raster at Temp
> after changing the scaling with the above patch, this looks nice for me:
>   d.legend -s range=0,13.5 labelnum=4 at=50,90,5,95 map=
> regards,
> Hamish

Paul T. Moen
pmoen at nd.gov
701-328-3696 (fax)
ND State Water Commission

More information about the grass-user mailing list