1 Quick Start
1.1 Overview
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.
1.2 Basic Plotting
After loading the correct module using (require plot) try
Any other function using the contract (-> real? real?) can be plotted using the same form. To plot multiple items, use the functions mix and mix* to combine the items to be plotted.
The display area and appearance of the plot can be changed by adding bracjets argument/value pairs after the first argument.
#: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.
#: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
(vector 2 2 2)))
can be interpreted in several ways:
As error data: (plot (error-bars data))
A function of two variables, such as
(define 3dfun (lambda (x y) (* (sin x) (sin y))))
can be plotted on a 2d graph
Using contours to represent height (z)
Using color shading
Using a gradient field
(plot (vector-field (gradient 3dfun)))
or in a 3d box
1.3 Curve Fitting
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
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,
(fit-result-final-params fitted)
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.
(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.
1.4 Creating Custom Plots
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.
For example, if we wanted to create a constructor that creates plot-items that draw dashed lines given a (-> real? real?) function, we could do the following:
(require plot/extend) |
|
(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]) |
(x-lists (build-list |
(/ segments 2) |
(lambda (index) |
(/ samples segments) |
dash-size))))))) |
(lambda (2dview) |
(send 2dview set-line-color color) |
(send 2dview set-line-width width) |
(lambda (dash) |
(send 2dview plot-line |
x-lists)))) |
Plot a test case using dashed-line:
(plot (dashed-line (lambda (x) x) #:color 'blue))