# colorbar

Plot gray scale or color scale bar

## Synopsis

gmt colorbar [ -B[p|s]parameters ] [ -C cpt ] [ -D refpoint ] [ -F panel ] [ -G zlo/zhi ] [ -I[max_intens|low_i/high_i] ] [ -J parameters ] [ -J z|Z parameters ] [ -L[i|I][gap] ] [ -M ] [ -N[p|dpi ]] [ -Q ] [ -R region ] [ -S[+a angle][+c|n][+r][+s][+x label][+y unit] ] [ -U[stamp] ] [ -V[level] ] [ -W scale ] [ -X[a|c|f|r][xshift] ] [ -Y[a|c|f|r][yshift] ] [ -Z widthfile ] [ -p flags ] [ -t transp ] [ --PAR=value ]

## Description

Plots gray scales or color scales on maps. Both horizontal and vertical scales are supported. For color palette tables (CPTs) with gradational colors (i.e., the lower and upper boundary of an interval have different colors) we will interpolate to give a continuous scale. Variations in intensity due to shading/illumination may be displayed by setting the option -I. Colors may be spaced according to a linear scale, all be equal size, or by providing a file with individual tile widths. The font used for the annotations along the scale is specified by FONT_ANNOT_PRIMARY while any unit placed at the side of the bar is controlled by FONT_ANNOT_SECONDARY. If a label along the bar is requested, it is plotted with FONT_LABEL. For a full overview of CPTs, see the Technical Reference section on Color palette tables.

Example of a horizontal color bar placed below a geographic map.

## Required Arguments

None.

## Optional Arguments

-B[p|s]parameters

  Set annotation, tick, and gridline interval for the bar. The x-axis label will plot beneath a horizontal bar (or vertically to the right of a vertical bar), except when using the +m modifier of the -D option. As an option, use the y-axis label to plot the data unit to the right of a horizontal bar (and above a vertical bar). If -B is omitted, or no annotation intervals are provided (classic mode only), the default is to annotate every color level based on the numerical entries in the CPT (which may be overridden by ULB flags in the CPT). The exception to this rule is for CPT files that were scaled to fit the range of a grid exactly and thus have arbitrary color levels; these will trigger an automatic -Baf setting. To specify custom text annotations for intervals, you must append ;annotation to each z-slice in the CPT. Note: The -B option relies on the -R and -J settings of the given hierarchical level to plot correctly. For standard -B operations, (See full description) (See cookbook information).

-C[cpt]

  cpt is the CPT to be used. If no cpt is appended or no -C is given then we use the current CPT (modern mode only). In classic mode, if no -C is given then we read standard input. By default all color changes are annotated. To use a subset, add an extra column to the CPT with a L, U, or B to annotate Lower, Upper, or Both color segment boundaries (but see -B). Like grdview, we can understand pattern specifications in the CPT. For CPTs where the z range is in meters, it may be useful to change to another unit when plotting. To do so, append +U unit to the file name. Likewise, if the CPT uses another unit than meter and you wish to plot the CPT versus meters, append +u unit. If a GMT master dynamic CPT is given instead then its z-range will be set to its default range (if it has one) before plotting.

-D[g|j|J|n|x]refpoint[+w length[/width]][+e[b|f][length]][+h|v][+j justify][+m[a|c|l|u]][+n[txt]][+o dx[/dy]][+r]

  > Defines the reference point on the map for the color scale using one of four coordinate systems:

  > - g - Append lon/lat for map coordinates. Requires both -R and -J to be set.

  > - j - Append code to j or J code for setting the refpoint via a 2-char justification code that refers to the (invisible) projected map bounding box. Requires both -R and -J to be set. Upper case means justify on the outside.

  > - n - Append xn/yn for normalized bounding box coordinates (0-1). Requires both -R and -J to be set.

  > - x - Append x/y for plot coordinates (append cm, inch, or ppoint).

  > For -Dj or -DJ with codes TC, BC, ML, MR (i.e., centered on one of the map sides) we pre-calculate all further settings. Specifically, the length is set to 80% of the map side, horizontal or vertical depends on the side, the offset is MAP_LABEL_OFFSET for Dj with an extra offset MAP_FRAME_WIDTH for DJ, and annotations are placed on the side of the scale facing away from the map frame. If not given, the default argument is JBC (Place color bar centered beneath current plot). However, you can override any of these with these modifiers:

  - +w followed by the length and width of the color bar sets bar dimensions. If width is not specified then it is set to 4% of the given length. If length is not given then it defaults to 80% of the corresponding map side dimension. If either length or width end with % then those percentages are used instead to set the dimensions, where width is defined as a percentage of the bar length.

  - +e adds sidebar triangles for back- and/or foreground colors. Append f (foreground) or b background) for only one sidebar triangle [Default gives both]. Optionally, append triangle height [Default is half the barwidth].

  - +h selects a horizontal scale [Default is vertical (+v)].

  - +j sets the anchor point. By default, the anchor point on the scale is assumed to be the bottom left corner (BL), but this can be changed by appending +j followed by a 2-char justification code justify (see text). Note: If -Dj is used then justify defaults to the same as refpoint, if -DJ is used then justify defaults to the mirror opposite of refpoint. Consequently, -DJ is used to place a scale outside the map frame while -Dj is used to place it inside the frame.

  - +m will move text to opposite side as per arguments [a|c|l|u]. Horizontal scale bars: Move annotations and labels above the scale bar [Default is below]; the unit remains on the left. Vertical scale bars: Move annotations and labels to the left of the scale bar [Default is to the right]; the unit remains below. Append one or more of a, l or u to control which of the annotations, label, and unit that will be moved to the opposite side. Append c if you want to print a vertical label as a column of characters (does not work with special characters).

  - +n plots a rectangle with the NaN color at the start of the bar, append text to change label from NaN.

  - +r will reverse the positive direction of the bar.

-F[+c clearances][+g fill][+i[[gap/]pen]][+p[pen]][+r[radius]][+s[[dx/dy/][shade]]]

  Without further options, draws a rectangular border around the bar using MAP_FRAME_PEN. The following modifiers can be appended to -F, with additional explanation and examples provided in the The background panel cookbook section:

  - +c - Set clearance where clearance is either gap, xgap/ygap, or lgap/rgap/bgap/tgap and gap gives a uniform clearance, xgap/ygap gives separate clearances in the x- and y- directions, and lgap/rgap/bgap/tgap gives individual clearances between the map embellishment and the border for each side.

  - +g - Give fill to fill the box with a color specified by fill [default is no fill].

  - +i - Draw a secondary, inner border border outline [[gap/]pen]. Optionally, append the gap between the inner and outer border and the pen for the inner border [default is a uniform gap between borders of 2p and the MAP_DEFAULT_PEN].

  - +p - append pen to specify different pen attributes for the box outline.

  - +r - Draw a rounded rectangular borders instead of sharp with a corner radius set by radius (append units) [defaults is 6p].

  - +s - Place an offset background shaded region behind the box. Here, dx/dy indicates the shift relative to the foreground frame [default is 4p/-4p] and shade sets the fill style to use for shading [default is gray50].

-G zlo/zhi

  Truncate the incoming CPT so that the lowest and highest z-levels are to zlo and zhi. If one of these equal NaN then we leave that end of the CPT alone. The truncation takes place before the plotting.

-I[max_intens|low_i/high_i]

  Add illumination effects. Optionally, set the range of intensities from -max_intens to +max_intens. If not specified, 1 is used. Alternatively, append low/high intensities to specify an asymmetric range [Default is no illumination].

-J parameters

  Specify the projection. (See full description) (See cookbook summary) (See projections table).

-L[i|I][gap]

> Gives equal-sized color rectangles. Default scales rectangles according to the z-range in the CPT (Also see -Z). If gap is appended and the CPT is discrete we will center each annotation on each rectangle, using the lower boundary z-value for the annotation. If i is prepended we annotate the interval range instead, and if I is used instead then we include the background and foreground values in the label (e.g, “< 12”). If -I is used then each rectangle will have its constant color modified by the specified intensity. Note: For categorical CPTs we default to activating -L with a gap such that the sum of all the gaps equal 15% of the bar width. You may chose no gaps by giving -L only or explicitly set gap = 0.

-M

  Force a monochrome graybar using the (television) YIQ transformation.

-N[p|dpi]

  Controls how the color scale should be encoded graphically. To preferentially draw color rectangles (e.g., for discrete colors), append p. Otherwise we will preferentially draw images (e.g., for continuous colors). Optionally append effective dots-per-inch for rasterization of color scales [600].

-Q

  Select logarithmic scale and power of ten annotations. All z-values in the CPT will be converted to p = \(\log_{10}(z)\) and only integer p values will be annotated using the 10^p format [Default is linear scale].

-R west/east/south/north[/zmin/zmax][+r][+u unit]

> Specify the region of interest.

> The region may be specified in one of several ways:

> 1. -R west/east/south/north. This is the standard way to specify geographic regions when using map projections where meridians and parallels are rectilinear. The coordinates may be specified in decimal degrees or in [±]dd:mm[:ss.xxx][W|E|S|N] format.

> 2. -R west/south/east/north+r. This form is useful for map projections that are oblique, making meridians and parallels poor choices for map boundaries. Here, we instead specify the lower left corner and upper right corner geographic coordinates, followed by the modifier +r. This form guarantees a rectangular map even though lines of equal longitude and latitude are not straight lines.

> 3. -R g or -R d. These forms can be used to quickly specify the global domain (0/360 for -Rg and -180/+180 for -Rd in longitude, with -90/+90 in latitude).

> 4. -R code1,code2,…[+e|r|R incs]. This indirectly supplies the region by consulting the DCW (Digital Chart of the World) database and derives the bounding regions for one or more countries given by the codes. Simply append one or more comma-separated countries using either the two-character ISO 3166-1 alpha-2 convention (e.g., NO) or the full country name (e.g., Norway). To select a state within a country (if available), append .state (e.g, US.TX), or the full state name (e.g., Texas). To specify a whole continent, spell out the full continent name (e.g., -RAfrica). Finally, append any DCW collection abbreviations or full names for the extent of the collection or named region. All names are case-insensitive. The following modifiers can be appended:

>    > - +r to adjust the region boundaries to be multiples of the steps indicated by inc, xinc/yinc, or winc/einc/sinc/ninc [default is no adjustment]. For example, -R FR+r1 will select the national bounding box of France rounded to nearest integer degree, where inc can be positive to expand the region or negative to shrink the region.

>    > - +R to adjust the region by adding the amounts specified by inc, xinc/yinc, or winc/einc/sinc/ninc [default is no extension], where inc can be positive to expand the region or negative to shrink the region.

>    > - +e to adjust the region boundaries to be multiples of the steps indicated by inc, xinc/yinc, or winc/einc/sinc/ninc, while ensuring that the bounding box is adjusted by at least 0.25 times the increment [default is no adjustment], where inc can be positive to expand the region or negative to shrink the region.

> 5. -R xmin/xmax/ymin/ymax[+u unit] specifies a region in projected units (e.g., UTM meters) where xmin/xmax/ymin/ymax are Cartesian projected coordinates compatible with the chosen projection (-J) and unit is an allowable distance unit [e]; we inversely project to determine the actual rectangular geographic region. For projected regions centered on (0,0) you may use the short-hand -R halfwidth[/halfheight]+u unit, where halfheight defaults to halfwidth if not given. This short-hand requires the +u modifier.

> 6. -R justify lon0/lat0/nx/ny, where justify is a 2-character combination of L|C|R (for left, center, or right) and T|M|B (for top, middle, or bottom) (e.g., BL for lower left). The two character code justify indicates which point on a rectangular region region the lon0/lat0 coordinates refer to and the grid dimensions nx and ny are used with grid spacings given via -I to create the corresponding region. This method can be used when creating grids. For example, -RCM 25/25/50/50 specifies a 50x50 grid centered on 25,25.

> 7. -R gridfile. This will copy the domain settings found for the grid in specified file. Note that depending on the nature of the calling module, this mechanism will also set grid spacing and possibly the grid registration (see Grid registration: The -r option).

> 8. -Ra[uto] or -Re[xact]. Under modern mode, and for plotting modules only, you can automatically determine the region from the data used. You can either get the exact area using -Re [Default if no -R is given] or a slightly larger area sensibly rounded outwards to the next multiple of increments that depend on the data range using -Ra.

> For perspective view -p, optionally append /zmin/zmax. (more …)

-S[+a angle][+c|n][+r][+s][+x label][+y unit]

  Control various aspects of color bar appearance when -B is not used (Note: If -L is used then -B cannot be used). The following modifiers are available:

  - +a Place annotations at the given angle [default is no slanting].

  - +c Use custom labels if given in the CPT as annotations.

  - +n Use numerical labels instead [Default]. Only one of +c and +n can be set.

  - +r Only annotate lower and upper limits in the CPT [Default follows CPT boundaries].

  - +s Skip drawing gridlines separating different color intervals [Default draws gridlines].

  - +x Place a bar label via +x label.

  - +y Place a bar unit via +y unit.

-U[label|+c][+j justify][+o dx[/dy]][+t text]

  Draw GMT time stamp logo on plot. (See full description) (See cookbook information).

-V[level]

  Select verbosity level [w]. (See full description) (See cookbook information).

-W scale

  Multiply all z-values in the CPT by the provided scale. By default the CPT is used as is.

-X[a|c|f|r][xshift]

  Shift plot origin. (See full description) (See cookbook information).

-Y[a|c|f|r][yshift]

  Shift plot origin. (See full description) (See cookbook information).

-Z widthfile

  File with bar-width per color entry. By default, width of entry is scaled to color range, i.e., z = 0-100 gives twice the width as z = 100-150 (Also see -L). Note: The widths may be in plot distance units or given as relative fractions and will be automatically scaled so that the sum of the widths equals the requested bar length.

-p[x|y|z]azim[/elev[/zlevel]][+w lon0/lat0[/z0]][+v x0/y0] (more …)

  Select perspective view. (Required -R and -J for proper functioning).

-t transp[/transp2] (more …)

  Set transparency level(s) in percent.

-^ or just -

  Print a short message about the syntax of the command, then exit (Note: on Windows just use -).

-+ or just +

  Print an extensive usage (help) message, including the explanation of any module-specific option (but not the GMT common options), then exit.

-? or no arguments

  Print a complete usage (help) message, including the explanation of all options, then exit.

--PAR=value

  Temporarily override a GMT default setting; repeatable. See gmt.conf for parameters.

## MacOS Preview Warning

Low-resolution raster-images appearing in PDF or PostScript files may look blurry when viewed with the Preview application under macOS. This happens because Preview decides to resample very coarse (low dpi) images instead of leaving them alone; we do not know of a simple way to turn this feature off. It is most noticeable in color bars for discrete CPTs (we now use -Np as the default setting for such CPTs) and very small grids plotted in either grdimage or grdview -Qi|c. However, if a raster format (such as JPG or PNG) is selected instead then here is no such blurring. Other PDF viewers (e.g., Adobe Acrobat) do not seem similarly affected.

## Examples

Note: Since many GMT plot examples are very short (i.e., one module call between the gmt begin and gmt end commands), we will often present them using the quick modern mode GMT Modern Mode One-line Commands syntax, which simplifies such short scripts.

To plot a horizontal color scale (12 cm long; 0.5 cm wide) at the reference point (8,1) (paper coordinates) with justification at top center and automatic annotation interval, do

    gmt begin map
      gmt makecpt -T-200/1000/100 -Crainbow
      gmt colorbar -Dx8c/1c+w12c/0.5c+jTC+h -Bxaf+l"topography" -By+lkm
    gmt end show

To append a vertical color scale (7.5 cm long; 1.25 cm wide) to the right of a plot that is 6 inch wide and 4 inch high, using illumination, and show back- and foreground colors, and annotating every 5 units, we provide the reference point and select the left-mid anchor point via

    gmt colorbar -Dx6.5i+jLM/2i+w7.5c/1.25c+e -Ccolors.cpt -I -Bx5+lBATHYMETRY -By+lm

To overlay a horizontal color scale (4 inches long and default width) above a Mercator map produced by a previous call, ensuring a 2 cm offset from the map frame, use

    gmt colorbar -DjCT+w4i+o0/2c+h -Ccolors.cpt -Baf

## Notes

1. When the CPT is discrete and no illumination is specified, the color bar will be painted using polygons. For all other cases we must paint with an image. Some color printers may give slightly different colors for the two methods given identical RGB values. See option -N for affecting these decisions. Also note that for years now, Apple’s Preview insists on smoothing deliberately course CPT color images to a blur. Use another PDF viewer if this bothers you.

2. For cyclic (wrapping) color tables the cyclic symbol is plotted to the right of the color bar. If annotations are specified there then we place the cyclic symbol at the left, unless +n was used in which case we center of the color bar instead.

3. Discrete CPTs may have transparency applied to all or some individual slices. Continuous CPTs may have transparency applied to all slices, but not just some.

## See Also

gmt, makecpt gmtlogo, grd2cpt image, legend