How to use the OCaml bindings

The three examples provided below illustrate the available methods for generating plots with PLplot from OCaml. They proceed in order from lowest-level to highest-level.

How to setup findlib for use with the OCaml bindings

The following examples require that findlib and its associated tools (i.e., ocamlfind) are installed in in your $PATH.

If PLplot was installed under a non-standard prefix, or any prefix where findlib does not check automatically for OCaml libraries, then the following environment variables can be set to tell findlib where to look for PLplot:

        export OCAMLPATH=$PLPLOT_INSTALL_PREFIX/lib/ocaml:$OCAMLPATH
        export LD_LIBRARY_PATH=$PLPLOT_INSTALL_PREFIX/lib/ocaml/stublibs:$LD_LIBRARY_PATH
      

Sample command line project (core API)

Here is a simple example that can be compiled and run from the command line. The result will be a program that generates a plot of part of a parabola using only the core PLplot API.

        (* Open the Plplot module to give access to all of the PLplot
           values without the need to add the "Plplot." prefix. *)
        open Plplot

        let simple_example () =
          (* Sample at 20 points, ranging from -10.0 to 10.0 *)
          let xs = Array.init 21 (fun xi -> float xi -. 10.0) in
          let ys = Array.map (fun x -> x**2.0) xs in

          (* Initialize PLplot *)
          plinit ();

          (* Draw the plot window axes *)
          plenv (-10.0) 10.0 0.0 100.0 0 0;

          (* Draw the parabola points as a series of line segments *)
          plline xs ys;

          (* End the plotting session *)
          plend ();
          ()

        let () = simple_example ()
      

Save this code as simple_example_core.ml. The following command can then be used to build the example:

        ocamlfind opt -package plplot -linkpkg -o simple_example_core simple_example_core.ml
      

The resulting binary program can be run by typing ./simple_example_core

Sample command line project (OCaml-specific API)

Here is another example that can be compiled and run from the command line. The result will be a program that generates a plot of part of a parabola similar to the above example, but now using the OCaml-specific PLplot API rather than the core PLplot API.

        (* Open the Plplot module to give access to all of the PLplot
           values without the need to add the "Plplot." prefix.
           Aliasing the module P to the module Plot will save some typing
           without further namespace pollution. *)
        open Plplot
        module P = Plot

        let simple_example () =
          (* Initialize a new plot, using the windowed Cairo device
             ("xcairo") *)
          let p =
            P.init (-10.0, 0.0) (10.0, 100.0) `greedy (`window `cairo)
          in

          (* Draw the parabola *)
          P.plot ~stream:p [P.func `blue (fun x -> x ** 2.0) (-10.0, 10.0)];

          (* Draw the plot axes and close up the plot stream using the default
             spacing between tick marks. *)
          P.finish ~stream:p ();
          ()

        let () = simple_example ()
      

Save this code as simple_example_ocaml.ml. The following command can then be used to build the example:

        ocamlfind opt -package plplot -linkpkg -o simple_example_ocaml simple_example_ocaml.ml
      

The resulting binary program can be run by typing ./simple_example_ocaml

Sample toplevel project

The OCaml interactive toplevel (ocaml) provides a very useful tool for code testing, development and interactive data analysis.

The Quick_plot module provides a set of functions for producing quick, simple two-dimensional plots from both the toplevel and stand-alone OCaml programs. Here is a set of commands which can be used in a toplevel session to produce a plot of a portion of a parabola, similar to the compiled examples above.

        # #use "topfind";;
        # #require "plplot";;
        # open Plplot;;
        # Quick_plot.func ~names:["Parabola"] [(fun x -> x ** 2.0)] (-10.0, 10.0);;
      

Conversely, the above ocaml session could be expressed in a compiled OCaml program:

        Plplot.Quick_plot.func ~names:["Parabola"] [(fun x -> x ** 2.0)] (-10.0, 10.0)
      

Save this code as simple_example_quick.ml. The following command can then be used to build the example:

        ocamlfind opt -package plplot -linkpkg -o simple_example_quick simple_example_quick.ml
      

The resulting binary program can be run by typing ./simple_example_quick