On this page:
1.1 Overview
1.2 Basic Plotting
1.3 Curve Fitting
1.4 Creating Custom Plots

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:

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:

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