On this page:
11.1 Plotting
plot
plot3d
points
line
error-bars
vector-field
contour
shade
surface
mix
plot-color?
11.2 Curve Fitting
fit
fit-result
11.3 Miscellaneous Functions
derivative
gradient
make-vec
Version: 5.2

11 Compatibility Module

Alexander Friedman,
Jamie Raymond,
and Neil Toronto

 (require plot/compat)

This module provides an interface compatible with PLoT 5.1.3 and earlier.

Do not use both plot and plot/compat in the same module. It is tempting to try it, to get both the new features and comprehensive backward compatibility. But to enable the new features, the objects plotted in plot have to be a different data type than the objects plotted in plot/compat. They do not coexist easily, and trying to make them do so will result in contract violations.

11.1 Plotting

(plot data    
  [#:width width    
  #:height height    
  #:x-min x-min    
  #:x-max x-max    
  #:y-min y-min    
  #:y-max y-max    
  #:x-label x-label    
  #:y-label y-label    
  #:title title    
  #:fgcolor fgcolor    
  #:bgcolor bgcolor    
  #:lncolor lncolor    
  #:out-file out-file])  (is-a?/c image-snip%)
  data : ((is-a?/c 2d-plot-area%) . -> . void?)
  width : real? = 400
  height : real? = 400
  x-min : real? = -5
  x-max : real? = 5
  y-min : real? = -5
  y-max : real? = 5
  x-label : string? = "X axis"
  y-label : string? = "Y axis"
  title : string? = ""
  fgcolor : (list/c byte? byte? byte?) = '(0 0 0)
  bgcolor : (list/c byte? byte? byte?) = '(255 255 255)
  lncolor : (list/c byte? byte? byte?) = '(255 0 0)
  out-file : (or/c path-string? output-port? #f) = #f
Plots data in 2D, where data is generated by functions like points or line.

A data value is represented as a procedure that takes a 2d-plot-area% instance and adds plot information to it.

The result is a image-snip% for the plot. If an #:out-file path or port is provided, the plot is also written as a PNG image to the given path or port.

The #:lncolor keyword argument is accepted for backward compatibility, but does nothing.

(plot3d data    
  [#:width width    
  #:height height    
  #:x-min x-min    
  #:x-max x-max    
  #:y-min y-min    
  #:y-max y-max    
  #:z-min z-min    
  #:z-max z-max    
  #:alt alt    
  #:az az    
  #:x-label x-label    
  #:y-label y-label    
  #:z-label z-label    
  #:title title    
  #:fgcolor fgcolor    
  #:bgcolor bgcolor    
  #:lncolor lncolor    
  #:out-file out-file])  (is-a?/c image-snip%)
  data : ((is-a?/c 3d-plot-area%) . -> . void?)
  width : real? = 400
  height : real? = 400
  x-min : real? = -5
  x-max : real? = 5
  y-min : real? = -5
  y-max : real? = 5
  z-min : real? = -5
  z-max : real? = 5
  alt : real? = 30
  az : real? = 45
  x-label : string? = "X axis"
  y-label : string? = "Y axis"
  z-label : string? = "Z axis"
  title : string? = ""
  fgcolor : (list/c byte? byte? byte?) = '(0 0 0)
  bgcolor : (list/c byte? byte? byte?) = '(255 255 255)
  lncolor : (list/c byte? byte? byte?) = '(255 0 0)
  out-file : (or/c path-string? output-port? #f) = #f
Plots data in 3D, where data is generated by a function like surface. The arguments alt and az set the viewing altitude (in degrees) and the azimuth (also in degrees), respectively.

A 3D data value is represented as a procedure that takes a 3d-plot-area% instance and adds plot information to it.

The #:lncolor keyword argument is accepted for backward compatibility, but does nothing.

(points vecs [#:sym sym #:color color])
  ((is-a?/c 2d-plot-area%) . -> . void?)
  vecs : (listof (vectorof real?))
  sym : (or/c char? string? exact-integer? symbol?) = 'square
  color : plot-color? = 'black
Creates 2D plot data (to be provided to plot) given a list of points specifying locations. The sym argument determines the appearance of the points. It can be a symbol, an ASCII character, or a small integer (between -1 and 127). The following symbols are known: 'pixel, 'dot, 'plus, 'asterisk, 'circle, 'times, 'square, 'triangle, 'oplus, 'odot, 'diamond, '5star, '6star, 'fullsquare, 'bullet, 'full5star, 'circle1, 'circle2, 'circle3, 'circle4, 'circle5, 'circle6, 'circle7, 'circle8, 'leftarrow, 'rightarrow, 'uparrow, 'downarrow.

(line f 
  [#:samples samples 
  #:width width 
  #:color color 
  #:mode mode 
  #:mapping mapping 
  #:t-min t-min 
  #:t-max t-max]) 
  ((is-a?/c 2d-plot-area%) . -> . void?)
  f : (real? . -> . (or/c real? (vector/c real? real?)))
  samples : (and/c exact-integer? (>=/c 2)) = 150
  width : (>=/c 0) = 1
  color : plot-color/c = 'red
  mode : (one-of/c 'standard 'parametric) = 'standard
  mapping : (one-of/c 'cartesian 'polar) = 'cartesian
  t-min : real? = -5
  t-max : real? = 5
Creates 2D plot data to draw a line.

The line is specified in either functional, i.e. y = f(x), or parametric, i.e. x,y = f(t), mode. If the function is parametric, the mode argument must be set to 'parametric. The t-min and t-max arguments set the parameter when in parametric mode.

(error-bars vecs [#:color color])
  ((is-a?/c 2d-plot-area%) . -> . void?)
  vecs : (listof (vector/c real? real? real?))
  color : plot-color? = 'black
Creates 2D plot data for error bars given a list of vectors. Each vector specifies the center of the error bar (x,y) as the first two elements and its magnitude as the third.

(vector-field f 
  [#:samples samples 
  #:width width 
  #:color color 
  #:style style]) 
  ((is-a?/c 2d-plot-area%) . -> . void?)
  f : ((vector/c real? real?) . -> . (vector/c real? real?))
  samples : (and/c exact-integer? (>=/c 2)) = 20
  width : exact-positive-integer? = 1
  color : plot-color? = 'red
  style : (one-of/c 'scaled 'normalized 'real) = 'scaled
Creates 2D plot data to draw a vector-field from a vector-valued function.

(contour f 
  [#:samples samples 
  #:width width 
  #:color color 
  #:levels levels]) 
  ((is-a?/c 2d-plot-area%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : exact-nonnegative-integer? = 50
  width : (>=/c 0) = 1
  color : plot-color/c = 'black
  levels : (or/c (and/c exact-integer? (>=/c 2)) (listof real?))
   = 10
Creates 2D plot data to draw contour lines, rendering a 3D function a 2D graph cotours (respectively) to represent the value of the function at that position.

(shade f [#:samples samples #:levels levels])
  ((is-a?/c 2d-plot-area%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : (and/c exact-integer? (>=/c 2)) = 50
  levels : (or/c (and/c exact-integer? (>=/c 2)) (listof real?))
   = 10
Creates 2D plot data to draw like contour, except using shading instead of contour lines.

(surface f 
  [#:samples samples 
  #:width width 
  #:color color]) 
  ((is-a?/c 3d-plot-area%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : (and/c exact-integer? (>=/c 2)) = 50
  width : (>=/c 0) = 1
  color : plot-color/c = 'black
Creates 3D plot data to draw a 3D surface in a 2D box, showing only the top of the surface.

(mix data ...)  (any/c . -> . void?)
  data : (any/c . -> . void?)
Creates a procedure that calls each data on its argument in order. Thus, this function can composes multiple plot datas into a single data.

(plot-color? v)  boolean?
  v : any/c
Returns #t if v is one of the following symbols, #f otherwise:

'white 'black 'yellow 'green 'aqua 'pink
'wheat 'grey 'blown 'blue 'violet 'cyan
'turquoise 'magenta 'salmon 'red

11.2 Curve Fitting

Do not use the fit function. It is going to be removed in Racket 5.2.1. It relies on old C code that nobody understands or is willing to maintain, and that is also slightly crashy.

Quite independent of plotting, and for reasons lost in the sands of time, the plot library provides a non-linear, least-squares fit algorithm to fit parameterized functions to given data. The code that implements the algorithm is public domain, and is used by the gnuplot package.

To fit a particular function to a curve:

A more realistic example can be found in "compat/tests/fit-demo-2.rkt" in the "plot" collection.

(fit f guess-list data)  fit-result?
  f : (real? ... . -> . real?)
  guess-list : (list/c (list symbol? real?))
  data : 
(or/c (list-of (vector/c real? real? real?))
      (list-of (vector/c real? real? real? real?)))
Do not use the fit function. It is going to be removed in Racket 5.2.1. It relies on old C code that nobody understands or is willing to maintain, and that is also slightly crashy.

Attempts to fit a fittable function to the data that is given. The guess-list should be a set of arguments and values. The more accurate your initial guesses are, the more likely the fit is to succeed; if there are no good values for the guesses, leave them as 1.

(struct fit-result (rms
    variance
    names
    final-params
    std-error
    std-error-percent
    function)
  #:extra-constructor-name make-fit-result)
  rms : real?
  variance : real?
  names : (listof symbol?)
  final-params : (listof real?)
  std-error : (listof real?)
  std-error-percent : (listof real?)
  function : (real? ... . -> . real?)
The params field contains an associative list of the parameters specified in fit and their values. Note that the values may not be correct if the fit failed to converge. For a visual test, use the function field to get the function with the parameters in place and plot it along with the original data.

11.3 Miscellaneous Functions

(derivative f [h])  (real? . -> . real?)
  f : (real? . -> . real?)
  h : real? = 1e-06
Creates a function that evaluates the numeric derivative of f. The given h is the divisor used in the calculation.

(gradient f [h])
  ((vector/c real? real?) . -> . (vector/c real? real?))
  f : (real? real? . -> . real?)
  h : real? = 1e-06
Creates a vector-valued function that the numeric gradient of f.

(make-vec fx fy)
  ((vector/c real? real?) . -> . (vector/c real? real?))
  fx : (real? real? . -> . real?)
  fy : (real? real? . -> . real?)
Creates a vector-values function from two parts.