GRASS logo


r.out.legend - Create and export image file with legend of a raster map


raster, color, color table, image


r.out.legend --help
r.out.legend [-fd] raster=name file=name filetype=extension dimensions=width,height [unit=unit] [resolution=value] [bgcolor=name] [labelnum=integer] [range=min,max] [digits=integer] [font=string] [fontsize=integer] [--overwrite] [--help] [--verbose] [--quiet] [--ui]


Flip legend
Add histogram to legend
Allow output files to overwrite existing files
Print usage summary
Verbose module output
Quiet module output
Force launching GUI dialog


raster=name [required]
Name of raster map
file=name [required]
Name of the output file
filetype=extension [required]
File type
Options: png, ps, cairo
dimensions=width,height [required]
width and height of the color legend
unit of the image dimensions
Options: cm, mm, inch, px
Default: cm
resolution (dots/inch)
background colour
Default: none
Number of text labels
Default: 5
Use a subset of the map range for the legend
Maximum number of digits for raster value display
Default: 1
Font name
Default: Arial
Font size
Default: 10

Table of contents


The r.out.legend script provides a convenient way to quickly create a smoothed legend image for floating point raster maps (continuous values as opposed to categories), with the dimensions and resolution required. Optionally, a histogram can be added along side the legend by setting the -d flag. It is furthermore possible to set the font (default is 'Arial') and font size (default=10). The legend can be saved as PNG, PS (postscript), BMP, PPM, PDF AND SVG. The latter four are supported through the Cairo driver (see Notes). The images can be used to add a continuous legends in maps created in e.g., QGIS composer or as part of a Google Earth map.


The user is required to set the dimensions of the legend bar. This does not include the category values, which will thus add to the size of the output image. The size of the output image in pixels and the unit set by the user is given in the console (see example 1).

Note that one can use category maps as input, but r.out.legend will still create a smooth legend (by setting the -f flag, see d.legend).

The user may create a horizontal legend by making the box wider than it is tall. Note that for vertical legends labels are placed to the right of the legend box. For horizontal legends the text will be places below the legend.

The -d flag is used to display a histogram distribution along side the legend. For vertical legends, the histogram will be placed left of the legend; if horizontal, it will be placed above the legend bar. The histogram is 1.75 x the width (or height if horizontal) of the legend bar. This thus adds to the final width of the output image. Note that the statistics are calculated on the current computational region settings set by g.region. The default range however covers the entire natural bounds of the input map. If the histogram appears empty, check your region settings.

The script is a wrapper of d.mon and d.legend. For detailed explanations of the different options, see the manual pages of these two functions. To find out the fonts available on your system, you can run d.font -l. For a more interactive way to generate legend images, you can also try

With file type set to 'png', the png driver is used to create the image. An alternative way to create a png image file is to set as file type 'cairo' and provide an output file name with as extension '.png'. This will use the cairo driver to generate the png image. One advantage of the cairo driver is that it uses anti-aliasing, which might give nicer results (smoother lines and numbers). Compare the output for example 1 and 4.

The default resolution is 300 px/inch (ppi), except when the unit is set to px, in which case the default resolution is set to 96 ppi.

It is important to realize that setting the resolution will determine the height and width of your image in pixels. It does not, however, set the image print resolution. Other programs, like Libreoffice, Word or the QGIS print composer will therefore not know the intended image print resolution. This means you will have to set the image size manually in the document. The exact dimention of the output file is given in the console.

The range option lets the user define the limits of the legend. Note however that the color scale will remain faithful to the values as defined with r.colors.

The Cairo driver generates PNG, BMP, PPM, PS, PDF or SVG images using the Cairo graphics library. The image format is selected from the extension of the output file. For this option to work, GRASS has to be configured with CAIRO support (if you compile GRASS yourself, use --with-cairo when configuring GRASS).


The raster layers in the examples below are from the North Carolina sample data set.

Example 1

Note that because width > height, the legend is printed horizontal, with the labels below the legend bar.
r.out.legend raster=elevation file=r_out_legend_1.png filetype=png \
    dimensions=4,0.4 labelnum=3 fontsize=7 unit="cm" resolution=150

The dimensions (width and height) of the image are printed to the console.

File saved as r_out_legend_1.png
The image dimensions are:
285px wide and 49px heigh
at a resolution of 150 ppi this is:
4.826 cm x 0.8382 cm

Example 2

Same as above, but the font is set to comic and the background color us set to grey.
r.out.legend raster=elevation file=r_out_legend_2.png filetype=png \
    dimensions=300,20 labelnum=3 fontsize=10 unit="px" \
    font=comic bgcolor=grey

Example 3

Like example 1, but without the raster values printed (done by setting fontsize to 0).
r.out.legend raster=elevation file=r_out_legend_3.png filetype=png \
    dimensions=300,20 labelnum=3 fontsize=0 unit="px"

Example 4

Like example 1, but using the cairo driver to create the png image. The difference is that the cairo driver uses anti-aliasing.
r.out.legend raster=elevation file=r_out_legend_4.png filetype=cairo \
    dimensions=4,0.4 labelnum=3 fontsize=7 unit="cm" resolution=150

The dimensions (width and height) of the image:

File saved as r_out_legend_4.png
The image dimensions are:
286px wide and 50px heigh
at a resolution of 150 ppi this is:
4.84293333333 cm x 0.855133333333 cm

Example 5

Like example 4, but adding a histogram. Note that the histogram adds to the size of the image (while the dimensions of the bar remain the same as in the previous example).
r.out.legend raster=elevation file=r_out_legend_5.png filetype=cairo \
    dimensions=4,0.4 labelnum=3 fontsize=7 unit="cm" resolution=150 -d

The dimensions (width and height) of the image:

File saved as r_out_legend_5.png
The image dimensions are:
286px wide and 92px heigh
at a resolution of 150 ppi this is:
4.84293333333 cm x 1.56633333333 cm


d.mon, d.legend, d.font, d.out.file,, r.category.trim


Paulo van Breugel, paulo at

Last changed: $Date: 2015-12-24 15:04:03 +0100 (Thu, 24 Dec 2015) $


Available at: r.out.legend source code (history)

Main index | Raster index | Topics index | Keywords index | Graphical index | Full index

© 2003-2017 GRASS Development Team, GRASS GIS 7.2.3svn Reference Manual