Version: 5.2
11 Compatibility Module
Alexander Friedman,
Jamie Raymond,
and Neil Toronto
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
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.
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.
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.
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.
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.
Creates 2D plot data to draw a vector-field from a vector-valued
function.
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.
Creates 2D plot data to draw like
contour, except using
shading instead of contour lines.
Creates 3D plot data to draw a 3D surface in a 2D box, showing only
the top of the surface.
Creates a procedure that calls each data on its argument in
order. Thus, this function can composes multiple plot datas
into a single data.
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:
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.
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.
(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.
A more realistic example can be found in
"compat/tests/fit-demo-2.rkt" in the "plot" collection.
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.
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
Creates a function that evaluates the numeric derivative of
f. The given h is the divisor used in the
calculation.
Creates a vector-valued function that the numeric gradient of
f.
Creates a vector-values function from two parts.