On this page:
2.1 Plotting
plot
plot3d
points
line
error-bars
vector-field
contour
shade
surface
mix
plot-color?
2.2 Curve Fitting
fit
fit-result
2.3 Miscellaneous Functions
derivative
gradient
make-vec

2 Plotting

 (require plot)

The plot library provides the ability to make basic plots, fit curves to data, and some useful miscellaneous functions.

2.1 Plotting

The plot and plot3d functions generate plots that can be viewed in the DrRacket interactions window.

(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-view%) . -> . 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 2-D, where data is generated by functions like points or lines.

A data value is represented as a procedure that takes a 2d-view% 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.

(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])  (is-a?/c image-snip%)
  data : ((is-a?/c 3d-view%) . -> . 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)
Plots data in 3-D, 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 3-D data value is represented as a procedure that takes a 3d-view% instance and adds plot information to it.

(points vecs [#:sym sym #:color color])
  ((is-a?/c 2d-view%) . -> . void?)
  vecs : (listof (vector/c real? real?))
  sym : (or/c character? integer? symbol?) = 'fullsquare
  color : plot-color? = 'black
Creates 2-D 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-min])  ((is-a?/c 2d-view%) . -> . void?)
  f : (real? . -> . (or/c real? (vector real? real?)))
  samples : exact-nonnegative-integer? = 150
  width : exact-positive-integer? = 1
  color : plot-color? = 'red
  mode : (one-of/c 'standard 'parametric) = 'standard
  mapping : (or-of/c 'cartesian 'polar) = 'cartesian
  t-min : real? = -5
  t-min : real? = 5
Creates 2-D 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-view%) . -> . void?)
  vecs : (listof (vector/c real? real? real?))
  color : plot-color? = 'black
Creates 2-D 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 
  [#:width width 
  #:color color 
  #:style style]) 
  ((is-a?/c 2d-view%) . -> . void?)
  f : ((vector real? real?) . -> . (vector real? real?))
  width : exact-positive-integer? = 1
  color : plot-color? = 'red
  style : (one-of/c 'scaled 'normalized 'read) = 'scaled
Creates 2-D 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-view%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : exact-nonnegative-integer? = 50
  width : exact-positive-integer? = 1
  color : plot-color? = 'black
  levels : 
(or/c exact-nonnegative-integer?
      (listof real?))
 = 10
Creates 2-D plot data to draw contour lines, rendering a 3-D function a 2-D graph cotours (respectively) to represent the value of the function at that position.

(shade f [#:samples samples #:levels levels])
  ((is-a?/c 2d-view%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : exact-nonnegative-integer? = 50
  levels : 
(or/c exact-nonnegative-integer?
      (listof real?))
 = 10
Creates 2-D 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-view%) . -> . void?)
  f : (real? real? . -> . real?)
  samples : exact-nonnegative-integer? = 50
  width : exact-positive-integer? = 1
  color : plot-color? = 'black
Creates 3-D plot data to draw a 3-D surface in a 2-D 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

2.2 Curve Fitting

PLoT uses the standard Non-Linear Least Squares fit algorithm for curve fitting. The code that implements the algorithm is public domain, and is used by the gnuplot package.

(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?)))
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.

2.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.