Skip to content

Commit 282d3e4

Browse files
committed
add plot examples
1 parent f4650c9 commit 282d3e4

File tree

13 files changed

+360
-9
lines changed

13 files changed

+360
-9
lines changed

.github/workflows/main.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ jobs:
6262
- name: Install owl-plplot
6363
run: opam exec -- dune build @install
6464

65-
#- name: Run tests
66-
# run: opam exec -- dune build
67-
# working-directory: examples
65+
- name: Run tests
66+
run: opam exec -- dune build
67+
working-directory: examples
6868

6969

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ all: build
55
build:
66
dune build @install
77

8-
.PHONY: test
9-
test:
10-
dune runtest -j 1 --no-buffer -p owl
8+
.PHONY: examples
9+
examples:
10+
cd examples && dune build
1111

1212
.PHONY: clean
1313
clean:

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,17 @@
22

33
[![build](https://github.com/owlbarn/owl-plplot/actions/workflows/main.yml/badge.svg)](https://github.com/owlbarn/owl-plplot/actions/workflows/main.yml)
44

5-
**Prerequisite** : Install the [Plplot](https://plplot.sourceforge.net/downloads.php) library
5+
This package aims to provide a rich set of easy-to-use plotting interface when using Owl for data analysis.
6+
7+
## Prerequisite
8+
9+
Install the [Plplot](https://plplot.sourceforge.net/downloads.php) library
10+
11+
## Install
12+
13+
Run `make install`
14+
15+
16+
## Build examples
17+
18+
Run `make examples`, and you can run each executables in `_build/default/examples/` and the view the generated figures in current directory.

examples/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build-examples:
2+
dune build

examples/box.ml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let y1 = Mat.uniform 1 10 in
6+
let y2 = Mat.uniform 10 100 in
7+
let h = Plot.create ~m:1 ~n:2 "plot_008.png" in
8+
Plot.subplot h 0 0;
9+
Plot.(bar ~h ~spec:[ RGB (0,153,51); FillPattern 3 ] y1);
10+
Plot.subplot h 0 1;
11+
Plot.(boxplot ~h ~spec:[ RGB (0,153,51) ] y2);
12+
Plot.output h
13+
14+
15+
let _ =
16+
let x = Mat.gaussian 200 1 in
17+
let h = Plot.create ~m:1 ~n:2 "plot_012.png" in
18+
Plot.subplot h 0 0;
19+
Plot.set_title h "histogram";
20+
Plot.histogram ~h ~bin:25 x;
21+
Plot.subplot h 0 1;
22+
Plot.set_title h "empirical cdf";
23+
Plot.ecdf ~h x;
24+
Plot.output h

examples/dune

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
(executables
22
(names
3-
mesh)
3+
box
4+
function
5+
lines
6+
mesh
7+
scatter
8+
stem
9+
subplot
10+
surf
11+
)
412
(libraries owl owl_plplot)
513
(flags ; in order to make the examples compile correctly even with the warnings.
614
(:standard

examples/function.ml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let f x = Maths.sin x /. x in
6+
let h = Plot.create "plot_001.png" in
7+
8+
Plot.set_title h "Function: f(x) = sine x / x";
9+
Plot.set_xlabel h "x-axis";
10+
Plot.set_ylabel h "y-axis";
11+
Plot.set_font_size h 8.;
12+
Plot.set_pen_size h 3.;
13+
Plot.plot_fun ~h f 1. 15.;
14+
15+
Plot.output h
16+
17+
18+
let _ =
19+
let x = Mat.logspace (-1.5) 2. 50 in
20+
let y = Mat.map Maths.exp x in
21+
let h = Plot.create ~m:2 ~n:2 "plot_013.png" in
22+
23+
Plot.subplot h 0 0;
24+
Plot.set_xlabel h "Input Data X";
25+
Plot.set_ylabel h "Input Data Y";
26+
Plot.(loglog ~h ~spec:[ RGB (0,255,0); LineStyle 2; Marker "+" ] ~x:x y);
27+
28+
Plot.subplot h 0 1;
29+
Plot.set_xlabel h "Index of Input Data Y";
30+
Plot.set_ylabel h "Input Data Y";
31+
Plot.(loglog ~h ~spec:[ RGB (0,0,255); LineStyle 1; Marker "*" ] y);
32+
33+
Plot.subplot h 1 0;
34+
Plot.set_xlabel h "Input Data X";
35+
Plot.set_ylabel h "Input Data Y";
36+
Plot.semilogx ~h ~x:x y;
37+
38+
Plot.subplot h 1 1;
39+
Plot.set_xlabel h "Index of Input Data Y";
40+
Plot.set_ylabel h "Input Data Y";
41+
Plot.semilogy ~h y;
42+
43+
Plot.output h

examples/lines.ml

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let h = Plot.create "plot_024.png" in
6+
Plot.(plot_fun ~h ~spec:[ RGB (0,0,255); Marker "#[0x2299]"; MarkerSize 8. ] Maths.sin 0. 9.);
7+
Plot.(plot_fun ~h ~spec:[ RGB (255,0,0); Marker "#[0x0394]"; MarkerSize 8. ] Maths.cos 0. 9.);
8+
Plot.legend_on h [|"Sine function"; "Cosine function"|];
9+
Plot.output h
10+
11+
12+
let _ =
13+
let g x = (Stats.gaussian_pdf x ~mu:0. ~sigma:1.) *. 100. in
14+
let y = Mat.gaussian ~mu:0. ~sigma:1. 1 1000 in
15+
16+
(* plot multiple data sets *)
17+
let h = Plot.create "plot_025.png" in
18+
Plot.set_background_color h 255 255 255;
19+
Plot.(histogram ~h ~spec:[ RGB (255,0,50) ] ~bin:100 y);
20+
Plot.(plot_fun ~h ~spec:[ RGB (0,0,255); LineWidth 2. ] g (-4.) 4.);
21+
Plot.legend_on h [|"data"; "model"|];
22+
23+
Plot.output h
24+
25+
26+
let _ =
27+
let x = Mat.(uniform 1 20 *$ 10.) in
28+
let y = Mat.(uniform 1 20) in
29+
let z = Mat.gaussian 1 20 in
30+
31+
(* plot multiple data sets *)
32+
let h = Plot.create "plot_026.png" in
33+
Plot.(plot_fun ~h ~spec:[ RGB (0,0,255); LineStyle 1; Marker "*" ] Maths.sin 1. 8.);
34+
Plot.(plot_fun ~h ~spec:[ RGB (0,255,0); LineStyle 2; Marker "+" ] Maths.cos 1. 8.);
35+
Plot.scatter ~h x y;
36+
Plot.stem ~h x z;
37+
38+
let u = Mat.(abs(gaussian 1 10 *$ 0.3)) in
39+
Plot.(bar ~h ~spec:[ RGB (255,255,0); FillPattern 3 ] u);
40+
41+
let v = Mat.(neg u *$ 0.3) in
42+
let u = Mat.sequential 1 10 in
43+
Plot.(area ~h ~spec:[ RGB (0,255,0); FillPattern 4 ] u v);
44+
45+
(* set up legend *)
46+
Plot.(legend_on h ~position:NorthEast [|"test 1"; "test 2"; "scatter"; "stem"; "bar"; "area"|]);
47+
Plot.output h
48+
49+
50+
let _ =
51+
let h = Plot.create "plot_004.png" in
52+
Plot.set_background_color h 255 255 255;
53+
Plot.set_pen_size h 2.;
54+
Plot.(draw_line ~h ~spec:[ LineStyle 1 ] 1. 1. 9. 1.);
55+
Plot.(draw_line ~h ~spec:[ LineStyle 2 ] 1. 2. 9. 2.);
56+
Plot.(draw_line ~h ~spec:[ LineStyle 3 ] 1. 3. 9. 3.);
57+
Plot.(draw_line ~h ~spec:[ LineStyle 4 ] 1. 4. 9. 4.);
58+
Plot.(draw_line ~h ~spec:[ LineStyle 5 ] 1. 5. 9. 5.);
59+
Plot.(draw_line ~h ~spec:[ LineStyle 6 ] 1. 6. 9. 6.);
60+
Plot.(draw_line ~h ~spec:[ LineStyle 7 ] 1. 7. 9. 7.);
61+
Plot.(draw_line ~h ~spec:[ LineStyle 8 ] 1. 8. 9. 8.);
62+
Plot.set_xrange h 0. 10.;
63+
Plot.set_yrange h 0. 9.;
64+
Plot.output h
65+
66+
67+
let _ =
68+
let h = Plot.create "plot_005.png" in
69+
70+
Array.init 9 (fun i ->
71+
let x0, y0 = 0.5, float_of_int i +. 1.0 in
72+
let x1, y1 = 4.5, float_of_int i +. 0.5 in
73+
Plot.(draw_rect ~h ~spec:[ FillPattern i ] x0 y0 x1 y1);
74+
Plot.(text ~h ~spec:[ RGB (0,255,0) ] 2.3 (y0-.0.2) ("pattern: " ^ (string_of_int i)))
75+
) |> ignore;
76+
77+
Plot.output h
78+
79+
80+
let _ =
81+
let x = Mat.linspace 0. 2. 100 in
82+
let y0 = Mat.sigmoid x in
83+
let y1 = Mat.map Maths.sin x in
84+
let h = Plot.create "plot_022.png" in
85+
Plot.(plot ~h ~spec:[ RGB (255,0,0); LineStyle 1; Marker "#[0x2299]"; MarkerSize 8. ] x y0);
86+
Plot.(plot ~h ~spec:[ RGB (0,255,0); LineStyle 2; Marker "#[0x0394]"; MarkerSize 8. ] x y1);
87+
Plot.(legend_on h ~position:SouthEast [|"sigmoid"; "sine"|]);
88+
Plot.output h
89+
90+
91+
let _ =
92+
let x = Mat.linspace 0. 6.5 20 in
93+
let y = Mat.map Maths.sin x in
94+
let h = Plot.create ~m:1 ~n:2 "plot_007.png" in
95+
Plot.set_background_color h 255 255 255;
96+
Plot.subplot h 0 0;
97+
Plot.plot_fun ~h Maths.sin 0. 6.5;
98+
Plot.(stairs ~h ~spec:[ RGB (0,128,255) ] x y);
99+
Plot.subplot h 0 1;
100+
Plot.(plot ~h ~spec:[ RGB (0,0,0) ] x y);
101+
Plot.(stairs ~h ~spec:[ RGB (0,128,255) ] x y);
102+
Plot.output h
103+
104+
105+
let _ =
106+
let x = Mat.linspace 0. 8. 100 in
107+
let y = Mat.map Maths.atan x in
108+
let h = Plot.create ~m:1 ~n:2 "plot_011.png" in
109+
Plot.subplot h 0 0;
110+
Plot.(area ~h ~spec:[ FillPattern 1 ] x y);
111+
let x = Mat.linspace 0. (2. *. 3.1416) 100 in
112+
let y = Mat.map Maths.sin x in
113+
Plot.subplot h 0 1;
114+
Plot.(area ~h ~spec:[ FillPattern 2 ] x y);
115+
Plot.output h

examples/mesh.ml

+26-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,29 @@ Plot.(mesh ~h ~spec:[ ZLine Y; Contour ] x y z);
2424
Plot.subplot h 1 2;
2525
Plot.(mesh ~h ~spec:[ ZLine XY; Curtain ] x y z);
2626

27-
Plot.output h
27+
Plot.output h
28+
29+
30+
let _ =
31+
let x, y = Mat.meshgrid (-3.) 3. (-3.) 3. 50 50 in
32+
let z = Mat.(
33+
3. $* ((1. $- x) **$ 2.) * exp (neg (x **$ 2.) - ((y +$ 1.) **$ 2.)) -
34+
(10. $* (x /$ 5. - (x **$ 3.) - (y **$ 5.)) * (exp (neg (x **$ 2.) - (y **$ 2.)))) -
35+
((1./.3.) $* exp (neg ((x +$ 1.) **$ 2.) - (y **$ 2.)))
36+
)
37+
in
38+
39+
let h = Plot.create ~m:2 ~n:3 "plot_016.png" in
40+
Plot.subplot h 0 0;
41+
Plot.surf ~h x y z;
42+
Plot.subplot h 0 1;
43+
Plot.mesh ~h x y z;
44+
Plot.subplot h 0 2;
45+
Plot.(surf ~h ~spec:[ Contour ] x y z);
46+
Plot.subplot h 1 0;
47+
Plot.(mesh ~h ~spec:[ Contour; Azimuth 115.; NoMagColor ] x y z);
48+
Plot.subplot h 1 1;
49+
Plot.(mesh ~h ~spec:[ Azimuth 115.; ZLine X; NoMagColor; RGB (61,129,255) ] x y z);
50+
Plot.subplot h 1 2;
51+
Plot.(mesh ~h ~spec:[ Azimuth 115.; ZLine Y; NoMagColor; RGB (130,255,40) ] x y z);
52+
Plot.output h

examples/scatter.ml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let x = Mat.uniform 1 30 in
6+
let y = Mat.uniform 1 30 in
7+
let h = Plot.create ~m:3 ~n:3 "plot_006.png" in
8+
Plot.set_background_color h 255 255 255;
9+
Plot.subplot h 0 0;
10+
Plot.(scatter ~h ~spec:[ Marker "#[0x2295]"; MarkerSize 5. ] x y);
11+
Plot.subplot h 0 1;
12+
Plot.(scatter ~h ~spec:[ Marker "#[0x229a]"; MarkerSize 5. ] x y);
13+
Plot.subplot h 0 2;
14+
Plot.(scatter ~h ~spec:[ Marker "#[0x2206]"; MarkerSize 5. ] x y);
15+
Plot.subplot h 1 0;
16+
Plot.(scatter ~h ~spec:[ Marker "#[0x229e]"; MarkerSize 5. ] x y);
17+
Plot.subplot h 1 1;
18+
Plot.(scatter ~h ~spec:[ Marker "#[0x2217]"; MarkerSize 5. ] x y);
19+
Plot.subplot h 1 2;
20+
Plot.(scatter ~h ~spec:[ Marker "#[0x2296]"; MarkerSize 5. ] x y);
21+
Plot.subplot h 2 0;
22+
Plot.(scatter ~h ~spec:[ Marker "#[0x2666]"; MarkerSize 5. ] x y);
23+
Plot.subplot h 2 1;
24+
Plot.(scatter ~h ~spec:[ Marker "#[0x22a1]"; MarkerSize 5. ] x y);
25+
Plot.subplot h 2 2;
26+
Plot.(scatter ~h ~spec:[ Marker "#[0x22b9]"; MarkerSize 5. ] x y);
27+
Plot.output h

examples/stem.ml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let x = Mat.linspace 0.5 2.5 25 in
6+
let y = Mat.map (Stats.exponential_pdf ~lambda:0.1) x in
7+
let h = Plot.create ~m:1 ~n:2 "plot_009.png" in
8+
Plot.set_background_color h 255 255 255;
9+
Plot.subplot h 0 0;
10+
Plot.set_foreground_color h 0 0 0;
11+
Plot.stem ~h x y;
12+
Plot.subplot h 0 1;
13+
Plot.(stem ~h ~spec:[ Marker "#[0x2295]"; MarkerSize 5.; LineStyle 1 ] x y);
14+
Plot.output h
15+
16+
17+
let _ =
18+
let x = Mat.linspace 0. 8. 30 in
19+
let y0 = Mat.map Maths.sin x in
20+
let y1 = Mat.uniform 1 30 in
21+
let h = Plot.create ~m:1 ~n:2 "plot_010.png" in
22+
Plot.subplot h 0 0;
23+
Plot.set_title h "Sine";
24+
Plot.autocorr ~h y0;
25+
Plot.subplot h 0 1;
26+
Plot.set_title h "Gaussian";
27+
Plot.autocorr ~h y1;
28+
Plot.output h

examples/subplot.ml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
open Owl
2+
open Owl_plplot
3+
4+
let _ =
5+
let f p i = match i with
6+
| 0 -> Stats.gaussian_rvs ~mu:0. ~sigma:0.5 +. p.(1)
7+
| _ -> Stats.gaussian_rvs ~mu:0. ~sigma:0.1 *. p.(0)
8+
in
9+
let y = Stats.gibbs_sampling f [|0.1;0.1|] 5_000 |> Mat.of_arrays in
10+
let h = Plot.create ~m:2 ~n:2 "plot_002.png" in
11+
Plot.set_background_color h 255 255 255;
12+
13+
(* focus on the subplot at 0,0 *)
14+
Plot.subplot h 0 0;
15+
Plot.set_title h "Bivariate model";
16+
Plot.scatter ~h (Mat.col y 0) (Mat.col y 1);
17+
18+
(* focus on the subplot at 0,1 *)
19+
Plot.subplot h 0 1;
20+
Plot.set_title h "Distribution of y";
21+
Plot.set_xlabel h "y";
22+
Plot.set_ylabel h "Frequency";
23+
Plot.histogram ~h ~bin:50 (Mat.col y 1);
24+
25+
(* focus on the subplot at 1,0 *)
26+
Plot.subplot h 1 0;
27+
Plot.set_title h "Distribution of x";
28+
Plot.set_ylabel h "Frequency";
29+
Plot.histogram ~h ~bin:50 (Mat.col y 0);
30+
31+
(* focus on the subplot at 1,1 *)
32+
Plot.subplot h 1 1;
33+
Plot.set_foreground_color h 255 0 0;
34+
Plot.set_title h "Sine function";
35+
Plot.(plot_fun ~h ~spec:[ LineStyle 2 ] Maths.sin 0. 28.);
36+
Plot.autocorr ~h (Mat.sequential 1 28);
37+
38+
(* output your final plot *)
39+
Plot.output h

0 commit comments

Comments
 (0)