On this page:
degrees->radians
radians->degrees
digits-for-range
real->plot-label
->plot-label
real->string/ trunc
linear-seq
linear-seq*
bounds->intervals
color-seq
color-seq*
->color
->pen-color
->brush-color
->pen-style
->brush-style
invertible-function
nonlinear-seq
mapped-function
kde
Version: 5.2

6 Plot Utilities

 (require plot/utils)

(degrees->radians d)  real?
  d : real?
Converts degrees to radians.

(radians->degrees r)  real?
  r : real?
Converts radians to degrees.

(digits-for-range x-min x-max [extra-digits])  exact-integer?
  x-min : real?
  x-max : real?
  extra-digits : exact-integer? = 3
Given a range, returns the number of decimal places necessary to distinguish numbers in the range. This may return negative numbers for large ranges.

Examples:

> (digits-for-range 0.01 0.02)

5

> (digits-for-range 0 100000)

-2

(real->plot-label x digits)  any
  x : real?
  digits : exact-integer?
Converts a real number to a plot label. Used to format axis tick labels, point-labels, and numbers in legend entries.

Examples:

> (let ([d  (digits-for-range 0.01 0.03)])
    (real->plot-label 0.02555555 d))

".02556"

> (real->plot-label 2352343 -2)

"2352300"

> (real->plot-label 1000000000.0 4)

"1e9"

> (real->plot-label 1000000000.1234 4)

"(1e9)+.1234"

(->plot-label a [digits])  string?
  a : any/c
  digits : exact-integer? = 7
Converts a Racket value to a label. Used by discrete-histogram and discrete-histogram3d.

(real->string/trunc x e)  string?
  x : real?
  e : exact-integer?
Like real->decimal-string, but removes trailing zeros and a trailing decimal point.

(linear-seq start    
  end    
  num    
  [#:start? start?    
  #:end? end?])  (listof real?)
  start : real?
  end : real?
  num : exact-nonnegative-integer?
  start? : boolean? = #t
  end? : boolean? = #t
Returns a list of evenly spaced real numbers between start and end. If start? is #t, the list includes start. If end? is #t, the list includes end.

This function is used internally to generate sample points.

Examples:

> (linear-seq 0 1 5)

'(0 1/4 1/2 3/4 1)

> (linear-seq 0 1 5 #:start? #f)

'(1/9 1/3 5/9 7/9 1)

> (linear-seq 0 1 5 #:end? #f)

'(0 2/9 4/9 2/3 8/9)

> (linear-seq 0 1 5 #:start? #f #:end? #f)

'(1/10 3/10 1/2 7/10 9/10)

(linear-seq* points    
  num    
  [#:start? start?    
  #:end? end?])  (listof real?)
  points : (listof real?)
  num : exact-nonnegative-integer?
  start? : boolean? = #t
  end? : boolean? = #t
Like linear-seq, but accepts a list of reals instead of a start and end. The #:start? and #:end? keyword arguments work as in linear-seq. This function does not guarantee that each inner value will be in the returned list.

Examples:

> (linear-seq* '(0 1 2) 5)

'(0 1/2 1 3/2 2)

> (linear-seq* '(0 1 2) 6)

'(0 2/5 4/5 6/5 8/5 2)

> (linear-seq* '(0 1 0) 5)

'(0 1/2 1 1/2 0)

(bounds->intervals xs)  (listof ivl?)
  xs : (listof real?)
Given a list of points, returns intervals between each pair.

Use this to construct inputs for rectangles and rectangles3d.

Example:

> (bounds->intervals (linear-seq 0 1 5))

(list (ivl 0 1/4) (ivl 1/4 1/2) (ivl 1/2 3/4) (ivl 3/4 1))

(color-seq c1 
  c2 
  num 
  [#:start? start? 
  #:end? end?]) 
  (listof (list/c real? real? real?))
  c1 : color/c
  c2 : color/c
  num : exact-nonnegative-integer?
  start? : boolean? = #t
  end? : boolean? = #t
Interpolates between colors—red, green and blue components separately—using linear-seq. The #:start? and #:end? keyword arguments work as in linear-seq.

Example:

> (plot (contour-intervals (λ (x y) (+ x y)) -2 2 -2 2
                           #:levels 4 #:contour-styles '(transparent)
                           #:colors (color-seq "red" "blue" 5)))

image

(color-seq* colors 
  num 
  [#:start? start? 
  #:end? end?]) 
  (listof (list/c real? real? real?))
  colors : (listof color/c)
  num : exact-nonnegative-integer?
  start? : boolean? = #t
  end? : boolean? = #t
Interpolates between colors—red, green and blue components separately—using linear-seq*. The #:start? and #:end? keyword arguments work as in linear-seq.

Example:

> (plot (contour-intervals (λ (x y) (+ x y)) -2 2 -2 2
                           #:levels 4 #:contour-styles '(transparent)
                           #:colors (color-seq* '(red white blue) 5)))

image

(->color c)  (list/c real? real? real?)
  c : color/c
Converts a non-integer plot color to an RGB triplet.

Symbols are converted to strings, and strings are looked up in a color-database<%>. Lists are unchanged, and color% objects are converted straightforwardly.

Examples:

> (->color 'navy)

'(36 36 140)

> (->color "navy")

'(36 36 140)

> (->color '(36 36 140))

'(36 36 140)

> (->color (make-object color% 36 36 140))

'(36 36 140)

This function does not convert integers to RGB triplets, because there is no way for it to know whether the color will be used for a pen or for a brush. Use ->pen-color and ->brush-color to convert integers.

(->pen-color c)  (list/c real? real? real?)
  c : plot-color/c
Converts a line color to an RGB triplet. This function interprets integer colors as darker and more saturated than ->brush-color does.

Non-integer colors are converted using ->color. Integer colors are chosen for good pairwise contrast, especially between neighbors. Integer colors repeat starting with 8.

Examples:

> (equal? (->pen-color 0) (->pen-color 8))

#t

> (plot (contour-intervals
         (λ (x y) (+ x y)) -2 2 -2 2
         #:levels 7 #:contour-styles '(transparent)
         #:colors (map ->pen-color (build-list 8 values))))

image

Converts a fill color to an RGB triplet. This function interprets integer colors as lighter and less saturated than ->pen-color does.

Non-integer colors are converted using ->color. Integer colors are chosen for good pairwise contrast, especially between neighbors. Integer colors repeat starting with 8.

Examples:

> (equal? (->brush-color 0) (->brush-color 8))

#t

> (plot (contour-intervals
         (λ (x y) (+ x y)) -2 2 -2 2
         #:levels 7 #:contour-styles '(transparent)
         #:colors (map ->brush-color (build-list 8 values))))

image

In the above example, mapping ->brush-color over the list is actually unnecessary, because contour-intervals uses ->brush-color internally to convert fill colors.

The function-interval function generally plots areas using a fill color and lines using a line color. Both kinds of color have the default value 3. The following example reverses the default behavior; i.e it draws areas using line color 3 and lines using fill color 3:
> (plot (function-interval sin (λ (x) 0) -4 4
                           #:color (->pen-color 3)
                           #:line1-color (->brush-color 3)
                           #:line2-color (->brush-color 3)
                           #:line1-width 4 #:line2-width 4))

image

Converts a symbolic pen style or a number to a symbolic pen style. Symbols are unchanged. Integer pen styles repeat starting at 5.

Examples:

> (eq? (->pen-style 0) (->pen-style 5))

#t

> (map ->pen-style '(0 1 2 3 4))

'(solid dot long-dash short-dash dot-dash)

Converts a symbolic brush style or a number to a symbolic brush style. Symbols are unchanged. Integer brush styles repeat starting at 7.

Examples:

> (eq? (->brush-style 0) (->brush-style 7))

#t

> (map ->brush-style '(0 1 2 3))

'(solid bdiagonal-hatch fdiagonal-hatch crossdiag-hatch)

> (map ->brush-style '(4 5 6))

'(horizontal-hatch vertical-hatch cross-hatch)

(struct invertible-function (f finv)
  #:extra-constructor-name make-invertible-function)
  f : (real? . -> . real?)
  finv : (real? . -> . real?)
Represents an invertible function.

The function itself is f, and its inverse is finv. Because real?s can be inexact, this invariant must be approximate and therefore cannot be enforced. (For example, (exp (log 10)) = 10.000000000000002.) The obligation to maintain it rests on whomever constructs one.

An axis transform such as plot-x-transform is a function from bounds start end to an invertible-function for which (f start) = start and (f end) = end (approximately), and the same is true of finv. The function f is used to transform points before drawing; its inverse finv is used to generate samples that will be evenly spaced after being transformed by f.

(Technically, because of the way PLoT uses invertible-function, f must only be a left inverse of finv; there is no requirement that f also be a right inverse of finv.)

(nonlinear-seq start    
  end    
  num    
  transform    
  [#:start? start?    
  #:end? end?])  (listof real?)
  start : real?
  end : real?
  num : exact-nonnegative-integer?
  transform : (real? real? . -> . invertible-function?)
  start? : boolean? = #t
  end? : boolean? = #t
Generates a list of reals that, if transformed using transform, would be evenly spaced. This is used to generate samples for transformed axes.

Examples:

> (linear-seq 1 10 4)

'(1 4 7 10)

> (nonlinear-seq 1 10 4 log-transform)

'(1.0 2.154434690031884 4.641588833612779 10.000000000000002)

> (parameterize ([plot-x-transform  log-transform])
    (plot (area-histogram sqr (nonlinear-seq 1 10 4 log-transform))))

image

(struct mapped-function (f fmap)
  #:extra-constructor-name make-mapped-function)
  f : (any/c . -> . any/c)
  fmap : ((listof any/c) . -> . (listof any/c))
Represents a function that maps over lists differently than (map f xs).

With some functions, mapping over a list can be done much more quickly if done specially. (An example is a piecewise function with many pieces that first must decide which interval its input belongs to. Deciding that for many inputs can be done more efficiently by sorting all the inputs first.) Renderer-producing functions that accept a (-> real? real?) also accept a mapped-function, and use its fmap to sample more efficiently.

(kde xs h)  
mapped-function?
(or/c real? #f)
(or/c real? #f)
  xs : (listof real?)
  h : real?
Given samples and a kernel bandwidth, returns a mapped-function representing a kernel density estimate, and bounds, outside of which the density estimate is zero. Used by density.