package prbnmcn-gnuplot
Install
Dune Dependency
Authors
Maintainers
Sources
md5=813665e1462ddd4bb538104e41a89d31
sha512=10a597bfbc89b1fe14f8e7005fd90a27871f58fba2bd1c2ef1c45d6bf8a5a3d7aeeaf1ea39e8409d6b7705987e0e017483a8cb61152c34cb705aad4f4578671f
doc/prbnmcn-gnuplot/Plot/index.html
Module Plot
Source
prbnmcn-gnuplot
: a self-contained interface to Gnuplot.
Introduction
We give an introduction to the library by giving some examples. These examples can be run by performing
> dune exec ./test/example.exe
in the root of the library.
Let's assume throughout we've access to some sampler for the standard normal distribution:
val normal : Random.State.t -> float
Data: 1d, 2d, 3d
The library encapsulates 1d, 2d and 3d data points using the types Plot.r1
, Plot.r2
and Plot.r3
. Similarly named functions allow to inject floating-point values into these types.
As an example, we can write samplers for 2d and 3d normal variates with specified means:
(** [st] is the random state *)
let normal2 (mu_x, mu_y)
st = Plot.r2 (normal st +. mu_x) (normal st +. mu_y)
Scatter plot example
Let's plot some clouds of points using normal2
. For 2d scatter plots, we use Plot.Scatter.points_2d
. We need to feed this function with a value of type r2 Plot.Data.t
. For instance, the following function specifies a 2d scatter plot with 100 random normally distributed points with mean mu
.
let scatter ~mu st =
Plot.Scatter.points_2d
~points:(Plot.Data.of_array (Array.init 100 (fun _ -> normal2 mu st)))
()
Let's produce a plot with two clouds around (3, 2)
and (10, 8)
and display it using the Plot.plot2
function.
let st = Random.State.make_self_init ()
let scatter2d_example =
Plot.plot2
~xaxis:"x"
~yaxis:"y"
~title:"Clouds"
[scatter ~mu:(3., 2.) st; scatter ~mu:(10., 8.) st]
Finally, we can produce the plot and save it, say to a .png file.
let () =
let target = Plot.png ~pixel_size:(1024, 1024) ~png_file:"scatter.png" () in
Plot.(run ~plot:scatter2d_example ~target exec)
3d plots work similarly, using the Plot.Scatter.points_3d
function.
Line plot example
The library also handles line plots, through the functions exposed in Plot.Line
. The API works similarly. Functions must be discretized before being plotted: for the sake of the example, let's introduce the following function.
let discretize f =
Array.init 100 (fun i ->
let x = float_of_int i *. 0.1 in
let y = f x in
Plot.r2 x y)
We prepare a fancy plot for the discretized sine function as follows.
let sine_with_points =
let open Plot in
Line.line_2d
~points:(Data.of_array (discretize sin))
~style:
Style.(default |> set_color Color.red |> set_point ~ptyp:Pointtype.box)
~with_points:true
()
We display it similarly as in the scatter plot case.
let line2d_example =
Plot.plot2 ~xaxis:"x" ~yaxis:"y" ~title:"Lines" [sine_with_points]
let () =
let target = Plot.png ~pixel_size:(1024, 1024) ~png_file:"lines.png" () in
Plot.(run ~plot:line2d_example ~target exec)
Histogram example
Finally, the library allows to prepare histograms.
let gaussian =
let open Plot in
Histogram.hist
~points:(Data.of_array (Array.init 100 (fun _ -> r1 @@ normal st)))
()
let histogram_example =
Plot.plot2 ~xaxis:"x" ~yaxis:"freq" ~title:"Histogram" [gaussian]
let () =
let target = Plot.png ~pixel_size:(1024, 1024) ~png_file:"histogram.png" () in
Plot.(run ~plot:histogram_example ~target exec)
The binning is performed automatically by Gnuplot: currently, the API does not provide any functionality for manually specifed box plots.
API
Raw data
r1, r2, r3
encode elements of R^n
for n
equal to 1, 2 or 3.
Colors, styles, etc
Tics. See the gnuplot documentation for the xtics
command for the meaning of these options.
Datasets
Plot specifications
'dim spec
is the type of a declarative plot specification for data of type 'dim
.
Scatter plots
Line plots
Histograms
Plot rendering
A plot
consists of
- a choice of
axes
(2d or 3d, with a name for each axis) - a list of plot
spec
s consistent with the dimension of theaxes
- a title
val plot2 :
xaxis:string ->
yaxis:string ->
?xtics:Tics.t ->
?ytics:Tics.t ->
?title:string ->
r2 spec list ->
plot
plot2 ~xaxis ~yaxis ?xtics ?ytics ?title specs
construct a 2d plot. The plots specs
are stacked together in the same figure. All the plots share the same 2d axis system, with names xaxis
and yaxis
.
val plot3 :
xaxis:string ->
yaxis:string ->
zaxis:string ->
?xtics:Tics.t ->
?ytics:Tics.t ->
?ztics:Tics.t ->
?title:string ->
r3 spec list ->
plot
plot3 ~xaxis ~yaxis ~zaxis ~title specs
construct a 3d plot. The plots specs
are stacked together in the same figure. All the plots share the same 3d axis system, with names xaxis
, yaxis
and zaxis
.
Plot target. Note that the Qt
and X11
targets do not work well with multiple ("matrix") plots on some tiling window managers or generally when resizing windows. In this case, it is recommended to use the Pdf
or Png
targets and use a third-party visualizer.
pdf ?cm_size ~pdf_file ()
specifies that the plot should be written to the file pdf_file
using the pdf format. The optional argument ?cm_size
specifies the width and height of the figure in cm
. If not specified, Gnuplot will select the aspect ratio and size automatically.
png ?pixel_size ~png_file ()
specifies that the plot should be written to the file png_file
using the PNG format. The optional argument ?pixel_size
specifies the width and height of the figure in pixels
. If not specified, Gnuplot will select the aspect ratio and size automatically.
The qt target. This target is interactive (it will open a window). The optional argument ?pixel_size
specifies the width and height of the figure in cm
. If not specified, Gnuplot will select the aspect ratio and size automatically.
The type of actions to perform with a plot.
save_to filename
saves the script to filename
(does not execute the script)
exec_and_save_to filename
executes the plot and also saves the script filename
run ?path ~plot ~target action
performs action
on plot
using target
.
- The optional argument
?path
defaults to"gnuplot"
.
For instance, to run some plot
to the qt
backend, one would perform the call:
run ~plot ~target:(qt ()) exec
val run_matrix :
?path:string ->
target:target ->
?title:string ->
action ->
plot option array array ->
unit
run_matrix ?path ~plots ~target action
performs action
on the plot matrix plots
using target
.
- The optional argument
?path
defaults to"gnuplot"
. - The optional argument
?title
specifies a title for the collection of sub-plots. It defaults to""
.