Syntax 1: Viewing a single palette

To display a single palette, type the palette command followed by the name of the palette. For example, to view the s1 color palette, type:

. colorpalette s1

The graph produced by colorpalette displays the colors as well as their names or RGB codes and, possibly, some additional information. Here is an example of a semantic palette with color codes and labels (see the lin palette):

. colorpalette lin, fruits

Likewise, when displaying a symbol palette using symbolpalette or a line pattern palette using linepalette, the names of the symbols or line pattern definitions are shown:

. symbolpalette default
. linepalette default

Syntax 2: Viewing multiple palettes

To display multiple palettes in a single graph, type the palette command, a colon, and a list of palettes separated by forward slashes:

. symbolpalette: default / lean / tufte / pplain / pblind

Names/codes and labels will not be displayed in this case. Options can be specified as overall options, but also separately for each palette. The options specified separately take precedence over the global options. Example:

. colorpalette, lcolor(black) n(6): ///
>     hcl, intense / hsv, intense / hcl, dark n(3) / hsv, dark n(3)

Graph option lcolor(black) draws black lines around the color fields, palette option n() determines the size of the palette. Options intense and dark select the type of hcl or hsv color scheme. Further hcl schemes are displayed in the next example, which also illustrates the effect of the vertical option:

. colorpalette, vertical n(10): ///
>     hcl, blues / hcl, greens / hcl, oranges / hcl, purples / hcl, heat / hcl, plasma

Retrieving palette elements

If used in single palette mode (syntax 1), the palette commands return the definitions of the palette elements in r(). r(p) will contain a space separated list of all elements, r(p1), r(p2), etc. will contain the single elements one by one. Here is an example of a line plot using the Set1 palette from ColorBrewer:

. sysuse uslifeexp, clear
(U.S. life expectancy, 1900-1999)

. foreach v in le_wfemale le_wmale le_bfemale le_bmale {
  2.     label variable `v' `"`=substr("`:var lab `v''",18,.)'"'
  3. }

. colorpalette Set1, select(1/3 5) nograph

. return list

                  r(n) =  4

              r(ptype) : "color"
              r(pname) : "Set1"
                  r(p) : ""228 26 28" "55 126 184" "77 175 74" "255 127 0""
                 r(p4) : "255 127 0"
                 r(p3) : "77 175 74"
                 r(p2) : "55 126 184"
                 r(p1) : "228 26 28"

. line le_wfemale le_wmale le_bfemale le_bmale year, lcolor(`r(p)') ///
>     lw(*2 ..) ytitle(Life expectancy)

Option select() is used to select specific colors from the palette. Here is another example that displays the language regions of Switzerland (using the spmap command by Maurizio Pisati; shape files and language data are from the Swiss Federal Statistical Office; the sfso palette implements some colors scheme used by the Swiss Federal Statistical Office):

. use towndata, clear

. colorpalette sfso, languages nograph

. spmap language using towns, id(_ID) fcolor(`r(p)') ocolor(`r(p)') ///
>     clmethod(unique) osize(vthin ..) ///
>     polygon(data(lakes) fcolor(ltblue*.8) ocolor(ebblue*.7) osize(vthin)) ///
>     line(data(cantons)) legend(pos(11))

In the above examples, qualitative color palettes were used because the data to be illustrated was categorical. For quantitative data, sequential or diverging color schemes are typically used. Here is an example illustrating the proportion of inhabitants who are on social welfare by Swiss municipalities, using the heat scale from the hsv palette. The scale goes from light yellow for low proportions to intense red for high proportions

. colorpalette hsv, heat n(6) reverse nograph

. spmap welfare using towns, id(_ID) fcolor(`r(p)') ocolor(`r(p)') ///
>     clmethod(custom) clbreaks(0 1 2 3 4 5 12) osize(vthin ..) ///
>     polygon(data(lakes) fcolor(ltblue*.8) ocolor(ebblue*.7) osize(vthin)) ///
>     line(data(cantons) color(gs6)) legend(pos(11)) 

Furthermore, here is an example using a polar (diverging) scheme (the pinkgreen scheme from the hcl palette). Green colors are used for below average welfare proportions, pink colors are used for above average proportions.

. summarize welfare, meanonly

. generate rd = welfare/r(mean)

. colorpalette hcl, pinkgreen n(8) reverse nograph

. spmap rd using towns, id(_ID) fcolor(`r(p)') ocolor(`r(p)') ///
>     clmethod(custom) clbreaks(0 .25 .5 .667 1 1.5 2 4 6.25) osize(vthin ..) ///
>     polygon(data(lakes) fcolor(ltblue*.8) ocolor(ebblue*.7) osize(vthin)) ///
>     line(data(cantons) color(gs6)) legend(pos(11)) 

Immediate palettes

Instead of using a named palette you can provide a custom palette by specifying a list of colorstyles (named colors, RBG colors specified as "# # #", CMYK colors specified as "# # # #", or HSV colors specified as "hsv # # #"). Here is an example displaying all named colors as listed in colorstyle (excluding black, white, and gray scales):

. colorpalette blue bluishgray brown cranberry cyan dimgray dkgreen dknavy  ///
>     dkorange eggshell emerald forest_green gold gray green khaki lavender ///
>     lime ltblue ltbluishgray ltkhaki magenta maroon midblue midgreen mint ///
>     navy olive olive_teal orange orange_red pink purple red sand sandb    ///
>     sienna stone teal yellow ebg ebblue edkblue eltblue eltgreen emidblue ///
>     erose, title(Named Stata colors)

In addition to the color specifications documented in colorstyle, you can specify colors using hex triplets. Here's an example displaying (approximate) FS 595 Safety Colors:

. colorpalette #bd1e24 #e97600 #f6c700 #007256 #0067a7 #964f8e, rows(6)

Here is another example illustrating the use of the intensity() option:

. colorpalette #bd1e24, intensity(0.1(.05)1)

Furthermore, HCL colors can be provided as "hcl # # #", where the first number specifies the hue (dominant wavelength in degrees of the 360 degree color wheel), the second number specifies the chroma (colorfulness) and the third number specifies the luminance (brightness). colorpalette will translate the HCL colors to RGB. Example:

. colorpalette "hcl 0 100 70" "hcl 120 100 70" "hcl 240 100 70"

See colorlist in the help file for colorpalette for further details.


Custom palettes

If you want to create an own named color palette, you can define a program called colorpalette_myname, where myname is the name of your palette. Your program should return the color definitions (e.g. RGB values or hex codes) as a comma-separated list in local macro P. In addition you may provide a comma-separated list of descriptions in local macro I. For example, the following program provides a palette called bootstrap3 containing the semantic colors used for buttons in Bootstrap v3.3:

. capture program drop colorpalette_bootstrap3

. program colorpalette_bootstrap3
  1.     c_local P #ffffff,#337ab7,#5cb85c,#5bc0de,#f0ad4e,#d9534f, ///
>               #e6e6e6,#286090,#449d44,#31b0d5,#ec971f,#c9302c
  2.     c_local I default,primary,success,info,warning,danger,default.focus, ///
>         primary.focus,success.focus,info.focus,warning.focus,danger.focus
  3. end

. colorpalette bootstrap3, rows(6)

More complicated definitions of palettes that take account of options are also possible. See the palette definitions in colorpalette.ado (type viewsource colorpalette.ado in Stata).

Likewise, a named symbol palette can be provided in program symbolpalette_myname, and a named line patter palette can be provided in program linepalette_myname. Here is a line pattern example:

. program linepalette_morse
  1.     c_local P .-###,-...###,-.-.###,-..###
  2.     c_local I A,B,C,D
  3. end

. linepalette morse