PLoT (aka PLTplot) provides a basic interface for producing common types of plots such as line and vector field plots as well as an advanced interface for producing customized plot types. Additionally, plots and plot-items are first-class values and can be generated in and passed to other programs.
|(plot (mix (line (lambda (x) (sin x)))|
|(line (lambda (x) (cos x)))))|
The display area and appearance of the plot can be changed by adding brackets argument/value pairs after the first argument.
|(plot (line (lambda (x) (sin x)))|
|#:x-min -1 #:x-max 1 #:title "Sin(x)")|
The appearance of each individual plot item can be altered by adding argument/value pairs after the data.
|(plot (line (lambda (x) x)|
|#:color 'green #:width 3))|
Besides plotting lines from functions in 2-D, the plotter can also render a variety of other data in several ways:
Discrete data, such as
(define data (list (vector 1 1 2) (vector 2 2 2)))
can be interpreted in several ways:
A function of two variables, such as
can be plotted on a 2d graph
Using contours to represent height (z)
Using color shading
Using a gradient field
or in a 3d box
The plot library uses a non-linear, least-squares fit algorithm to fit parameterized functions to given data.
To fit a particular function to a curve:
Set up the independent and dependent variable data. The first item in each vector is the independent variable, the second is the result. The last item is the weight of the error; we can leave it as 1 since all the items weigh the same.
(define data '(#(0 3 1) #(1 5 1) #(2 7 1) #(3 9 1) #(4 11 1)))
Set up the function to be fitted using fit. This particular function looks like a line. The independent variables must come before the parameters.
(define fit-fun (lambda (x m b) (+ b (* m x))))
If possible, come up with some guesses for the values of the parameters. The guesses can be left as one, but each parameter must be named.
Do the fit; the details of the function are described in Curve Fitting.
(define fitted (fit fit-fun '((m 1) (b 1)) data))
View the resulting parameters; for example,
will produce (2.0 3.0).
For some visual feedback of the fit result, plot the function with the new parameters. For convenience, the structure that is returned by the fit command has already the function.
(plot (mix (points data) (line (fit-result-function fitted))) #:y-max 15)
A more realistic example can be found in "demos/fit-demo-2.ss" in the "plot" collection.
Defining custom plots is simple: a plot-item (that is passed to plot or mix) is just a function that acts on a view. Both the 2-D and 3-D view snip have several drawing functions defined that the plot-item can call in any order. The full details of the view interface can be found in Customizing Plots.
|(define (dashed-line fun|
|#:x-min [x-min -5]|
|#:x-max [x-max 5]|
|#:samples [samples 100]|
|#:segments [segments 20]|
|#:color [color 'red]|
|#:width [width 1])|
|(let* ((dash-size (/ (- x-max x-min) segments))|
|(/ segments 2)|
|(/ samples segments)|
|(+ x-min (* 2 index dash-size))|
|(+ x-min (* (add1 (* 2 index))|
|(send 2dview set-line-color color)|
|(send 2dview set-line-width width)|
|(send 2dview plot-line|
|(map (lambda (x) (vector x (fun x))) dash)))|
Plot a test case using dashed-line: