Additional Colormaps for the Racket Plot Package
1 Using Colormaps
2 Colormaps Defined in this Package
make-tol-rainbow-colormap
3 Colormap Utilities
pp-color-map
pp-color-maps
7.6

Additional Colormaps for the Racket Plot Package

Alex Harsáyi

The plot package introduced support for color maps using the plot-pen-color-map and plot-brush-color-map and they allow producing visually atractive plots by using pre-selected colors which work well together.

Several color maps are defined inside the plot package plus additional ones can be defined using register-color-map. This package defines additional color maps to be used with the plot pakage.

1 Using Colormaps

To use color maps for plots, set the a color map using plot-pen-color-map and use integer indexes for #:color argument of various plot renderers. For example, this code will produce the plot below and changing the argument of plot-pen-color-map will automatically change the colors of the plot:

(require plot)
(parameterize ([plot-pen-color-map 'set1])
    (plot (list (function sin -5 5 #:color 0)
                (function cos -5 5 #:color 1))))

image

The color-map-names function can be used to retrieve the list of installed color maps and you can use the pp-color-map and pp-color-maps functions to inspect the colors available in a color map. Here are all the colors from the 'set1 color map used in the previous plot:

image

Color maps were designed for use with the plot package, and retrieving individual colors from a color map is perhaps unnecessarily complicated. You can use ->pen-color for this purpose like so:

(match-define (list red green blue)
  (parameterize ([plot-pen-color-map 'set1])
    (->pen-color 0)))

2 Colormaps Defined in this Package

 (require colormaps/tol) package: colormaps

The color maps defined in colormaps/tol are based on Paul Tol’s color schemes, see the link for more detailed description about the design of these color maps and their intented uses.

Requiring the colormaps/tol or colormaps modules will automatically register these color maps for use.

Qualitative Colour Maps should be used for nominal or categorical data, where magnitude differences are not relevant, for example lines in plots. The following qualitative color maps are defined:

Tag

Name

Colors

'tol-bq

Bright

image

'tol-hcq

High Contrast

image

'tol-vq

Vibrant

image

'tol-mq

Muted

image

'tol-pq

Pale

image

'tol-dq

Dark

image

'tol-lq

Light

image

Diverging Colour Maps should be used for data that is ordered between two extremes where the midpoint is important (e.g. positive and negative deviations from a mean value). The following diverging colour maps are defined:

Tag

Name

Colors

'tol-sd

Sunset

image

'tol-bd

BuRd

image

'tol-pd

PRGn

image

Sequential Colour Maps should be used for data that is ordered from a low to a high value. The following sequential color maps are defined:

Tag

Name

Colors

'tol-ys

YlOrBr

image

'tol-is

Iridescent

image

procedure

(make-tol-rainbow-colormap color-count 
  [#:register? register? 
  #:name name]) 
  (listof (list integer? integer? integer?))
  color-count : exact-positive-integer?
  register? : boolean? = #f
  name : (or/c #f symbol?) = #f
Create and maybe register a rainbow color scheme containing exactly color-count visually distinct colors. Returns the list of the colors in the color scheme as RGB triplets.

The color scheme is constructed as described Paul Tol’s notes about sequential color schemes. For color schemes up to 29 colors, the colors are selected from a discrete color palette, for larger number of colours the colours are interpolated.

When register? is #t, the color map will be registered using register-color-map using either a generated name or an user supplied one.

name specifies the name of the color scheme to use when registering it. When #f, a default name will be created by appending the number of colors to the symbol 'tol-rainbow. For example, the default name for a 10 color rainbow will be 'tol-rainbow-10.

Here are the first 10 color schemes created by calling make-tol-rainbow-colormap with arguments from 1 to 10. The function can generate color maps with larger number of colors as well, but the larger the number of colors, the less distinct adjacent ones will be.

Tag

Colors

'tol-rainbow-1

image

'tol-rainbow-2

image

'tol-rainbow-3

image

'tol-rainbow-4

image

'tol-rainbow-5

image

'tol-rainbow-6

image

'tol-rainbow-7

image

'tol-rainbow-8

image

'tol-rainbow-9

image

'tol-rainbow-10

image

3 Colormap Utilities

 (require colormaps/utils) package: colormaps

procedure

(pp-color-map name    
  [#:width width    
  #:height height    
  #:show-text? show-text?    
  #:text-style text-style    
  #:text-size text-size])  pict?
  name : symbol?
  width : (or/c #f positive?) = #f
  height : positive? = 30
  show-text? : boolean? = #t
  text-style : text-style/c = null
  text-size : (integer-in 1 1024) = 12
Return a pict representing the color map name, one cell for each color, possibly numbered for reference.

width is the total width of the resulting picture. When f, the width is determined from the number of colors in the color map and the height of each cell. When width is a number, this will be the total width of the pict, and cells will be scalled to fit inside this width.

height is the height of the resulting picture, and the height of each individual color cell.

show-text?, when #t, will label each cell with a number representing the index of the color in the color map. This number can be passd to the #:color parameter of plot renderers to use that color.

text-style is the text style used to render the text inside each cell. Passed to the text pict constructor.

text-size is the text size used to render the text inside each cell. Passed to the text pict constructor.

For example to print the colors for the built-in 'set1 color map, to print out the colors in 'set1, use (pp-color-map 'set1):

image

procedure

(pp-color-maps names    
  [#:width width    
  #:height height    
  #:show-text? show-text?    
  #:text-style text-style    
  #:text-size text-size])  pict?
  names : ((listof symbol?) (color-map-names))
  width : (or/c #f positive?) = #f
  height : positive? = 30
  show-text? : boolean? = #t
  text-style : text-style/c = null
  text-size : (integer-in 1 1024) = 12
Return a pict representing the color maps in names, one line for each color map, along with its name.

names defaults to all installed color maps. The width, height, show-text?, text-style and text-size arguments are the same as for pp-color-map.

For example to print the color maps for the built-in 'set1 and 'set2 color maps, use (pp-color-maps '(set1 set2)):

image