▼ |  | PLo T: Graph Plotting

1 | Quick Start
2 | Plotting
3 | Customizing Plots

► | 1 | Quick Start

1.1 | Overview
1.2 | Basic Plotting
1.3 | Curve Fitting
1.4 | Creating Custom Plots

On this page:

1.1`` Overview
1.2`` Basic Plotting
1.3`` Curve Fitting
1.4`` Creating Custom Plots

top ← prev up next →

### 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

( plot ( line ( lambda (x) x)))

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.

( 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:

  - As points: ( plot ( points data))

  - 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)

    ( plot ( contour 3dfun))

  - Using color shading

    ( plot ( shade 3dfun))

  - Using a gradient field

    ( plot ( vector-field ( gradient 3dfun)))

  or in a 3d box

  - Displaying only the top of the surface

    ( plot3d ( surface 3dfun))

#### 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
  ( 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,

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

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

#### 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])
( let* ((dash-size ( / ( - x-max x-min) segments))
(x-lists ( build-list
( / segments 2)
( lambda (index)
( x-values
( / samples segments)
( + x-min ( * 2 index dash-size))
( + x-min ( * ( add1 ( * 2 index))
dash-size)))))))
( lambda (2dview)
( send 2dview set-line-color color)
( send 2dview set-line-width width)
( for-each
( lambda (dash)
( send 2dview plot-line
( map ( lambda (x) ( vector x (fun x))) dash)))
x-lists))))

Plot a test case using dashed-line:

( plot (dashed-line ( lambda (x) x) #:color 'blue))

top ← prev up next →