Source file owl_plot.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
(** Plot Module *)
type dsmat = Owl_dense_matrix.D.mat
type color = RED | GREEN | BLUE
type legend_typ = LINE | SCATTER | BOX
type legend_position = North | South | West | East | NorthWest | NorthEast | SouthWest | SouthEast
type legend_item = {
mutable plot_type : legend_typ;
mutable line_style : int;
mutable line_color : int * int * int;
mutable marker : string;
mutable marker_color : int * int * int;
mutable fill_pattern : int;
mutable fill_color : int * int * int;
}
type page = {
mutable title : string;
mutable fgcolor : int * int * int;
mutable fontsize : float;
mutable is_3d : bool;
mutable no_axes : bool;
mutable xlabel : string;
mutable ylabel : string;
mutable zlabel : string;
mutable xrange : float * float;
mutable yrange : float * float;
mutable zrange : float * float;
mutable auto_xrange : bool;
mutable auto_yrange : bool;
mutable auto_zrange : bool;
mutable xticklabels : (float * string) list;
mutable yticklabels : (float * string) list;
mutable zticklabels : (float * string) list;
mutable xlogscale : bool;
mutable ylogscale : bool;
mutable xgrid : bool;
mutable ygrid : bool;
mutable zgrid : bool;
mutable altitude : float;
mutable azimuth : float;
mutable legend : bool;
mutable legend_position : legend_position;
mutable legend_items : legend_item array;
mutable legend_names : string array;
mutable plots : (unit -> unit) array;
}
type handle = {
mutable holdon : bool;
mutable output : string;
mutable bgcolor : int * int * int;
mutable pensize : float;
mutable page_size : int * int;
mutable shape : int * int;
mutable pages : page array;
mutable current_page : int;
}
type axis = X | Y | Z | XY | XZ | YZ | XYZ
type spec =
| RGB of int * int * int
| LineStyle of int
| LineWidth of float
| Marker of string
| MarkerSize of float
| Fill
| FillPattern of int
| Contour
| Altitude of float
| Azimuth of float
| ZLine of axis
| NoMagColor
| Curtain
| Faceted
| Axis of axis
let _get_rgb l default_val =
let l = l
|> List.filter (function RGB _ -> true | _ -> false)
|> List.map (function RGB (r,g,b) -> (r,g,b) | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_line_style l default_val =
let l = l
|> List.filter (function LineStyle _ -> true | _ -> false)
|> List.map (function LineStyle x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_line_width l default_val =
let l = l
|> List.filter (function LineWidth _ -> true | _ -> false)
|> List.map (function LineWidth x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_marker l default_val =
let l = l
|> List.filter (function Marker _ -> true | _ -> false)
|> List.map (function Marker x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_marker_size l default_val =
let l = l
|> List.filter (function MarkerSize _ -> true | _ -> false)
|> List.map (function MarkerSize x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_fill l default_val =
let l = l
|> List.filter (function Fill -> true | _ -> false)
|> List.map (function Fill -> true | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_fill_pattern l default_val =
let l = l
|> List.filter (function FillPattern _ -> true | _ -> false)
|> List.map (function FillPattern x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_contour l default_val =
let l = l
|> List.filter (function Contour -> true | _ -> false)
|> List.map (function Contour -> true | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_altitude l default_val =
let l = l
|> List.filter (function Altitude _ -> true | _ -> false)
|> List.map (function Altitude x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_azimuth l default_val =
let l = l
|> List.filter (function Azimuth _ -> true | _ -> false)
|> List.map (function Azimuth x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_zline l default_val =
let l = l
|> List.filter (function ZLine _ -> true | _ -> false)
|> List.map (function
| ZLine X -> Plplot.PL_DRAW_LINEX
| ZLine Y -> Plplot.PL_DRAW_LINEY
| ZLine XY -> Plplot.PL_DRAW_LINEXY
| _ -> default_val
)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_mag_color l default_val =
let l = l
|> List.filter (function NoMagColor -> true | _ -> false)
|> List.map (function NoMagColor -> false | _ -> true)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_curtain l default_val =
let l = l
|> List.filter (function Curtain -> true | _ -> false)
|> List.map (function Curtain -> true | _ -> false)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_faceted l default_val =
let l = l
|> List.filter (function Faceted -> true | _ -> false)
|> List.map (function Faceted -> true | _ -> false)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _get_axis l default_val =
let l = l
|> List.filter (function Axis _ -> true | _ -> false)
|> List.map (function Axis x -> x | _ -> default_val)
in
let k = List.length l in
if k = 0 then default_val else List.nth l (k - 1)
let _create_page () = {
title = "";
fgcolor = (0, 0, 0);
fontsize = -1.;
is_3d = false;
no_axes = false;
xlabel = "x";
ylabel = "y";
zlabel = "z";
xrange = (infinity, neg_infinity);
yrange = (infinity, neg_infinity);
zrange = (infinity, neg_infinity);
auto_xrange = true;
auto_yrange = true;
auto_zrange = true;
xticklabels = [];
yticklabels = [];
zticklabels = [];
xlogscale = false;
ylogscale = false;
xgrid = false;
ygrid = false;
zgrid = false;
altitude = 33.;
azimuth = 45.;
legend = false;
legend_position = NorthEast;
legend_items = [||];
legend_names = [||];
plots = [||];
}
let _create_handle () = {
holdon = true;
output = "";
bgcolor = (255, 255, 255);
pensize = 0.;
page_size = (0,0);
shape = (1, 1);
current_page = 0;
pages = [|_create_page ()|];
}
let create ?(m=1) ?(n=1) s =
let pages = Array.make (m * n) None |> Array.map (fun _ -> _create_page ()) in
let h = _create_handle () in
h.shape <- (m, n);
h.pages <- pages;
h.output <- s;
h
let _default_handle =
let h = _create_handle () in
h.holdon <- false;
h
let _supported_device = [
"aqt"; "xwin"; "pdf"; "ps"; "psc";
"png"; "svg"; "xfig"; "psttf"; "psttc";
"xcairo"; "pdfcairo"; "epscairo"; "pscairo"; "svgcairo";
"pngcairo"; "memcairo"; "extcairo"
]
let _set_device h =
try let x = Owl_utils.get_suffix h.output in
Plplot.plsdev x;
if String.length h.output = 0 then () else
Plplot.plsfnam h.output;
with _exn -> ()
let _add_legend_item p plot_type line_style line_color marker marker_color fill_pattern fill_color =
let item = {
plot_type = plot_type;
line_style = line_style;
line_color = line_color;
marker = marker;
marker_color = marker_color;
fill_pattern = fill_pattern;
fill_color = fill_color;
}
in
p.legend_items <- Array.append p.legend_items [|item|]
let _plplot_position pos =
let open Plplot in
match pos with
| North -> [ PL_POSITION_TOP ]
| South -> [ PL_POSITION_BOTTOM ]
| West -> [ PL_POSITION_LEFT ]
| East -> [ PL_POSITION_RIGHT ]
| NorthWest -> [ PL_POSITION_TOP; PL_POSITION_LEFT ]
| NorthEast -> [ PL_POSITION_TOP; PL_POSITION_RIGHT ]
| SouthWest -> [ PL_POSITION_BOTTOM; PL_POSITION_LEFT ]
| SouthEast -> [ PL_POSITION_BOTTOM; PL_POSITION_RIGHT ]
let _draw_legend p =
let open Plplot in
let _ = plscmap0n 64 in
let cbase = 16 in
let opt = [ PL_LEGEND_BOUNDING_BOX ] in
let position = _plplot_position p.legend_position @ [ PL_POSITION_INSIDE ] in
let opt_array = Array.map (fun item ->
match item.plot_type with
| LINE -> [ PL_LEGEND_LINE; PL_LEGEND_SYMBOL ]
| SCATTER -> [ PL_LEGEND_SYMBOL ]
| BOX -> [ PL_LEGEND_COLOR_BOX ]
) p.legend_items in
let text_colors = Array.map (fun _ -> 1) p.legend_items in
let text = Array.mapi (fun i _ -> p.legend_names.(i)) p.legend_items in
let line_colors = Array.mapi (fun i x ->
let r, g, b = x.line_color in
plscol0 (i + cbase) r g b; (i + cbase)
) p.legend_items in
let line_styles = Array.map (fun x -> x.line_style) p.legend_items in
let line_widths = Array.map (fun _ -> 1.) p.legend_items in
let marker_colors = line_colors in
let marker_scales = Array.map (fun _ -> 1.) p.legend_items in
let marker_nums = Array.map (fun _ -> 3) p.legend_items in
let markers = Array.map (fun x -> x.marker) p.legend_items in
let box_colors = line_colors in
let box_patterns = Array.map (fun x -> x.fill_pattern) p.legend_items in
let box_scales = Array.map (fun _x -> 0.8) p.legend_items in
let box_linewidths = Array.map (fun _x -> 1.) p.legend_items in
let _ = pllegend opt position 0.05 0.05
0.1 15 1 1 0 0
opt_array 1.0 1.0 2.0
1.0 text_colors text
box_colors box_patterns box_scales box_linewidths
line_colors line_styles line_widths
marker_colors marker_scales marker_nums markers
in ()
let _calculate_paper_size m n =
let max_w, max_h = 900., 900. in
let r0 = 4. /. 3. in
let cur_w, cur_h = r0 *. (float_of_int n), float_of_int m in
let r1 = max_w /. max_h in
let r2 = cur_w /. cur_h in
let w, h = match (r1 /. r2) < 1. with
| true -> max_w, max_w /. r2
| false -> max_h *. r2, max_h
in
int_of_float w, int_of_float h
let _config_2d_axis p =
let base = 0 in
if p.no_axes then -1 else
let residual =
if (p.xlogscale, p.ylogscale) = (true, false) then 10 else
if (p.xlogscale, p.ylogscale) = (false, true) then 20 else
if (p.xlogscale, p.ylogscale) = (true, true) then 30 else
if p.xticklabels |> List.length > 0
|| p.yticklabels |> List.length > 0
then 70
else 0
in base + residual
let _initialise h =
let open Plplot in
_set_device h;
let r, g, b = h.bgcolor in
plscolbg r g b;
let m, n = h.shape in
if not (h.shape = (1,1)) then plssub n m;
let x, y = match h.page_size = (0, 0) with
| true -> _calculate_paper_size m n
| false -> h.page_size
in
plspage 0. 0. x y 0 0;
plinit ();
plwidth h.pensize
let _draw_ticklabels p axis value =
let open Plplot in
let l = match axis with
| PL_X_AXIS -> p.xticklabels
| PL_Y_AXIS -> p.yticklabels
| PL_Z_AXIS -> p.zticklabels
in
try List.assoc value l
with _exn -> Printf.sprintf "%g" value
let _prepare_page p =
let open Plplot in
plslabelfunc (_draw_ticklabels p);
let r, g, b = p.fgcolor in
plscol0 2 r g b;
plcol0 2;
if p.fontsize > 0. then plschr p.fontsize 1.0;
let xmin, xmax = p.xrange in
let ymin, ymax = p.yrange in
let zmin, zmax = p.zrange in
let alt, az = p.altitude, p.azimuth in
if not p.is_3d then (
plenv xmin xmax ymin ymax 0 (_config_2d_axis p)
)
else (
pladv 0;
plvpor 0.0 1.0 0.0 0.9;
plwind (-1.0) 1.0 (-1.0) 1.5;
plw3d 1.0 1.0 1.2 xmin xmax ymin ymax zmin zmax alt az;
plbox3 "bntu" p.xlabel 0.0 0
"bntu" p.ylabel 0.0 0
"bcdfntu" p.zlabel 0.0 4
);
pllab p.xlabel p.ylabel p.title;
plcol0 1;
if p.legend then _draw_legend p
let _finalise () =
_default_handle.pages <- [|_create_page ()|];
Plplot.plend ()
let output h =
h.holdon <- false;
_initialise h;
Array.iteri (fun i p ->
if i > 0 then Plplot.pladv i;
_prepare_page p;
Array.iter (fun f -> f ()) p.plots
) h.pages;
_finalise ()
let set_output h s =
let x = Owl_utils.get_suffix s in
match (List.mem x _supported_device) with
| true -> h.output <- s
| false -> Owl_log.error "unsupported file type."
let get_output h = h.output
let set_title h s = (h.pages.(h.current_page)).title <- s
let set_xlabel h s = (h.pages.(h.current_page)).xlabel <- s
let set_ylabel h s = (h.pages.(h.current_page)).ylabel <- s
let set_zlabel h s = (h.pages.(h.current_page)).zlabel <- s
let set_xrange h a b =
(h.pages.(h.current_page)).auto_xrange <- false;
(h.pages.(h.current_page)).xrange <- (a, b)
let set_yrange h a b =
(h.pages.(h.current_page)).auto_yrange <- false;
(h.pages.(h.current_page)).yrange <- (a, b)
let set_zrange h a b =
(h.pages.(h.current_page)).auto_zrange <- false;
(h.pages.(h.current_page)).zrange <- (a, b)
let set_xticklabels h l = (h.pages.(h.current_page)).xticklabels <- l
let set_yticklabels h l = (h.pages.(h.current_page)).yticklabels <- l
let set_zticklabels h l = (h.pages.(h.current_page)).zticklabels <- l
let set_foreground_color h r g b = (h.pages.(h.current_page)).fgcolor <- (r, g, b)
let set_background_color h r g b = h.bgcolor <- (r, g, b)
let set_font_size h x = (h.pages.(h.current_page)).fontsize <- x
let set_pen_size h x = h.pensize <- x
let set_page_size h x y = h.page_size <- (x, y)
let legend_on h ?(position=NorthEast) s =
(h.pages.(h.current_page)).legend <- true;
(h.pages.(h.current_page)).legend_position <- position;
(h.pages.(h.current_page)).legend_names <- s
let legend_off h = (h.pages.(h.current_page)).legend <- false
let rgb = None [@@warning "-32"]
let text ?(h=_default_handle) ?(spec=[]) x y ?(dx=0.) ?(dy=0.) s =
let open Plplot in
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b; plcol0 1;
plptex x y dx dy 0. s;
plscol0 1 r' g' b'; plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let _thinning x =
let l = float_of_int (Array.length x) in
let n = if l < 16. then l else 16. in
let c = float_of_int (Array.length x) /. n in
Array.init (int_of_float n) (fun i -> x.(int_of_float (float_of_int i *. c)))
let _union_range p r x =
let a, b = r in
let m, n = Owl_stats.minmax x in
let c = if a < m then a else m in
let d = if b > n then b else n in
let e = (d -. c) *. p in
c -. e, d +. e
let _adjust_range ?(margin=0.) h d axis =
let p = h.pages.(h.current_page) in
match axis with
| X -> if p.auto_xrange then p.xrange <- _union_range margin p.xrange d
| Y -> if p.auto_yrange then p.yrange <- _union_range margin p.yrange d
| Z -> if p.auto_zrange then p.zrange <- _union_range margin p.zrange d
| _ -> failwith "owl_plot:_adjust_range"
let plot ?(h=_default_handle) ?(spec=[]) x y =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "" in
let marker_size = _get_marker_size spec 4. in
let line_style = _get_line_style spec 1 in
let line_width = _get_line_width spec (-1.) in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b; plcol0 1;
if line_width > (-1.) then plwidth line_width;
let c' = plgchr () |> fst in
plschr marker_size 1.;
if line_style > 0 && line_style < 9 then
pllsty line_style; plline x y;
if marker <> "" then (
let x', y' = _thinning x, _thinning y in
plstring x' y' marker
);
plschr c' 1.;
plwidth old_pensize;
pllsty 1;
plscol0 1 r' g' b'; plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p LINE line_style color marker color 0 color;
if not h.holdon then output h
let plot_fun ?(h=_default_handle) ?(spec=[]) f a b =
let x = Owl_dense_matrix.D.linspace a b 100 in
let y = Owl_dense_matrix.D.map f x in
plot ~h ~spec x y
let scatter ?(h=_default_handle) ?(spec=[]) x y =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "•" in
let marker_size = _get_marker_size spec 4. in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
let c' = plgchr () |> fst in
plschr marker_size 1.;
plstring x y marker;
plschr c' 1.;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p SCATTER 0 color marker color 0 color;
if not h.holdon then output h
let histogram ?(h=_default_handle) ?(spec=[]) ?(bin=10) x =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
_adjust_range h x X;
let xmin, xmax = Owl_stats.minmax x in
let ymin, ymax = 0., Owl_stats.((
histogram (`N bin) x).counts |> Array.map float_of_int |> max) *. 1.1 in
_adjust_range h [|xmin; xmax|] X;
_adjust_range h [|ymin; ymax|] Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
let _ = plscol0 1 r g b; plcol0 1 in
plhist x xmin xmax bin [ PL_HIST_DEFAULT; PL_HIST_NOSCALING; PL_HIST_NOEXPAND ];
plscol0 1 r' g' b'; plcol0 1;
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p BOX 1 color "" color 0 color;
if not h.holdon then output h
let subplot h i j =
let _, n = h.shape in
h.current_page <- (n * i + j)
let stem ?(h=_default_handle) ?(spec=[]) x y =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "#[0x2299]" in
let marker_size = _get_marker_size spec 4. in
let line_style = _get_line_style spec 2 in
let line_width = _get_line_width spec (-1.) in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b; plcol0 1;
if line_width > (-1.) then plwidth line_width;
let c' = plgchr () |> fst in
plschr marker_size 1.;
if line_style > 0 && line_style < 9 then (
pllsty line_style;
Owl_utils.Array.iter2 (fun x' y' -> pljoin x' 0. x' y') x y
);
if not (marker = "") then plstring x y marker;
plschr c' 1.;
plwidth old_pensize;
pllsty 1;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p LINE line_style color marker color 0 color;
if not h.holdon then output h
let autocorr ?(h=_default_handle) ?(spec=[]) x =
let z = Owl_dense_matrix.D.to_array x in
let x' = Array.init (Array.length z) (fun i -> float_of_int i) in
let y' = Array.mapi (fun i _ -> Owl_stats.autocorrelation ~lag:i z) x' in
let x' = Owl_dense_matrix.D.of_arrays [|x'|] in
let y' = Owl_dense_matrix.D.of_arrays [|y'|] in
set_xlabel h "Lag";
set_ylabel h "Autocorrelation";
let p = h.pages.(h.current_page) in
let r, g, b = _get_rgb spec p.fgcolor in
let color = RGB (r, g, b) in
let marker = Marker (_get_marker spec "•") in
let marker_size = MarkerSize (_get_marker_size spec 4.) in
let line_style = LineStyle (_get_line_style spec 1) in
let line_width = LineWidth (_get_line_width spec (-1.)) in
let spec = [ color; marker; marker_size; line_style; line_width ] in
stem ~h ~spec x' y'
let draw_line ?(h=_default_handle) ?(spec=[]) x0 y0 x1 y1 =
let open Plplot in
let x = [|x0; x1|] in
let y = [|y0; y1|] in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let line_width = _get_line_width spec (-1.) in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
if line_width > (-1.) then plwidth line_width;
if line_style > 0 && line_style < 9 then (
pllsty line_style;
pljoin x0 y0 x1 y1
);
plwidth old_pensize;
pllsty 1;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let plot_multi = None [@@warning "-32"]
let _draw_error_bar ?(w=0.) x y e =
let open Plplot in
let w = w /. 2. in
let x' = [|x; x|] in
let y' = [|y-.e; y+.e|] in
plline x' y';
let x' = [|x-.w; x+.w|] in
let y' = [|y+.e; y+.e|] in
plline x' y';
let x' = [|x-.w; x+.w|] in
let y' = [|y-.e; y-.e|] in
plline x' y'
let error_bar ?(h=_default_handle) ?(spec=[]) x y e =
let open Plplot in
let ymin = Owl_dense_matrix.D.(min'(y - e)) in
let ymax = Owl_dense_matrix.D.(max'(y + e)) in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
let e = Owl_dense_matrix.D.to_array e in
_adjust_range h x X;
_adjust_range h [|ymin; ymax|] Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let line_width = _get_line_width spec (-1.) in
let old_pensize = h.pensize in
let w = let a, b = Owl_stats.minmax x in (a -. b) *. 0.02 in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
if line_width > (-1.) then plwidth line_width;
pllsty line_style;
Owl_utils.Array.iter3 (fun x0 y0 e0 ->
_draw_error_bar ~w x0 y0 e0
) x y e;
plwidth old_pensize;
pllsty 1;
plscol0 1 r' g' b';
plcol0 1
) in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let _draw_whiskers_box w x y =
let open Plplot in
let ymed, y1st, y3rd = Owl_stats.(median y, first_quartile y, third_quartile y) in
let w = w /. 2. in
let x' = [|x-.w; x-.w; x+.w; x+.w; x-.w|] in
let y' = [|y1st; y3rd; y3rd; y1st; y1st|] in
pllsty 1; plline x' y';
let x' = [|x-.w; x+.w|] in
let y' = [|ymed; ymed|] in
pllsty 1; plline x' y';
let ymin, ymax = Owl_stats.minmax y in
let x' = [|x; x|] in
let y' = [|ymin; y1st|] in
pllsty 1; plline x' y';
let x' = [|x; x|] in
let y' = [|y3rd; ymax|] in
pllsty 1; plline x' y'
let boxplot ?(h=_default_handle) ?(spec=[]) y =
let open Plplot in
let m, _ = Owl_dense_matrix.D.shape y in
let x = Array.init m (fun i -> float_of_int i +. 1.) in
let xmin, xmax = Owl_stats.minmax x in
let w = 0.4 in
let y0 = Owl_dense_matrix.D.to_array y in
let y1 = Owl_dense_matrix.D.to_arrays y in
_adjust_range h [|xmin-.w; xmax+.w|] X;
_adjust_range h ~margin:0.1 y0 Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
Owl_utils.Array.iter2 (fun x' y' -> _draw_whiskers_box w x' y') x y1;
plscol0 1 r' g' b';
plcol0 1;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let _draw_bar w x0 y0 =
let open Plplot in
let x = [|x0-.w; x0-.w; x0+.w; x0+.w|] in
let y = [|0.; y0; y0; 0.|] in
plfill x y;
pllsty 1;
plline x y
let draw_rect ?(h=_default_handle) ?(spec=[]) x0 y0 x1 y1 =
let open Plplot in
let x = [|x0; x0; x1; x1|] in
let y = [|y1; y0; y0; y1|] in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let fill_pattern = _get_fill_pattern spec 0 in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
pllsty line_style;
plpsty fill_pattern;
plfill x y;
plscol0 1 r' g' b';
plcol0 1;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let bar ?(h=_default_handle) ?(spec=[]) y =
let open Plplot in
let w = 0.4 in
let y = Owl_dense_matrix.D.to_array y in
let x = Array.mapi (fun i _ -> float_of_int i +. 1.) y in
let xmin, xmax = Owl_stats.minmax x in
_adjust_range h [|xmin-.w; xmax+.w|] X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let fill_pattern = _get_fill_pattern spec 0 in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
pllsty line_style;
plpsty fill_pattern;
Owl_utils.Array.iter2 (fun x0 y0 -> _draw_bar w x0 y0) x y;
plscol0 1 r' g' b';
plcol0 1;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p BOX line_style color "" color fill_pattern color;
if not h.holdon then output h
let area ?(h=_default_handle) ?(spec=[]) x y=
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
let xmin, xmax = Owl_stats.minmax x in
let x = Array.(append (append [|xmin|] x) [|xmax|]) in
let y = Array.(append (append [|0.|] y) [|0.|]) in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let fill_pattern = _get_fill_pattern spec 0 in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
pllsty line_style;
plline x y;
plpsty fill_pattern;
plfill x y;
plscol0 1 r' g' b';
plcol0 1;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p BOX line_style color "" color fill_pattern color;
if not h.holdon then output h
let draw_polygon ?(h=_default_handle) ?(spec=[]) x y =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let fill_pattern = _get_fill_pattern spec 0 in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
pllsty line_style;
plline x y;
plpsty fill_pattern;
plfill x y;
plscol0 1 r' g' b';
plcol0 1;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p BOX line_style color "" color fill_pattern color;
if not h.holdon then output h
let _ecdf_interleave x i =
let m = Array.length x in
let n = 2 * m in
let y = Array.make n 0. in
Array.iteri (fun j z ->
let k = 2 * j + i in
if k < n then y.(k) <- z;
if k < n - 1 then y.(k + 1) <- z
) x;
y
let ecdf ?(h=_default_handle) ?(spec=[]) x =
let x0 = Owl_dense_matrix.D.to_array x in
let x, y = Owl_stats.ecdf x0 in
let x = _ecdf_interleave x 0 in
let y = _ecdf_interleave y 1 in
let n = Array.length x in
let x = Owl_dense_matrix.D.of_array x n 1 in
let y = Owl_dense_matrix.D.of_array y n 1 in
plot ~h ~spec x y
let stairs ?(h=_default_handle) ?(spec=[]) x y =
let x = Owl_dense_matrix.D.to_array x in
let y = Owl_dense_matrix.D.to_array y in
let x = _ecdf_interleave x 0 in
let a = y.(0) in
let y = _ecdf_interleave y 1 in
let _ = y.(0) <- a in
let n = Array.length x in
let x = Owl_dense_matrix.D.of_array x n 1 in
let y = Owl_dense_matrix.D.of_array y n 1 in
plot ~h ~spec x y
let draw_circle ?(h=_default_handle) ?(spec=[]) x y rr =
let open Plplot in
let n = 1000 in
let theta = (2. *. Owl_const.pi) /. (float_of_int n) in
let x' = Array.init (n + 1) (fun i -> x +. Owl_maths.(sin (float_of_int i *. theta)) *. rr) in
let y' = Array.init (n + 1) (fun i -> y +. Owl_maths.(cos (float_of_int i *. theta)) *. rr) in
_adjust_range h ~margin:0.05 x' X;
_adjust_range h ~margin:0.05 y' Y;
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let line_style = _get_line_style spec 1 in
let line_width = _get_line_width spec (-1.) in
let fill_pattern = _get_fill_pattern spec 0 in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
if line_width > (-1.) then plwidth line_width;
pllsty line_style;
plpsty fill_pattern;
plfill x' y';
plline x' y';
plscol0 1 r' g' b';
plcol0 1;
plwidth old_pensize;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let _draw_arc _fill n x =
let open Plplot in
let a = 2. *. Owl_const.pi in
let theta = a /. n in
let i = ref 0. in
Array.iter (fun y ->
let c = n *. y in
let x' = Array.init (int_of_float c + 1) (fun j -> Owl_maths.(sin ((float_of_int j +. !i) *. theta))) in
let y' = Array.init (int_of_float c + 1) (fun j -> Owl_maths.(cos ((float_of_int j +. !i) *. theta))) in
let x' = Array.(append [|0.|] x') in
let y' = Array.(append [|0.|] y') in
plline x' y';
let r', g', b' = plgcol0 1 in
let rgb = mod_float ((!i +. 1.) *. 50.) 256. |> int_of_float in
plscol0 1 rgb rgb rgb;
plcol0 1;
plpsty 3;
plfill x' y';
plscol0 1 r' g' b';
plcol0 1;
i := !i +. c;
) x
let pie ?(h=_default_handle) ?(spec=[]) x =
let open Plplot in
_adjust_range h ~margin:0.1 [|-1.; 1.|] X;
_adjust_range h ~margin:0.1 [|-1.; 1.|] Y;
let x = Owl_dense_matrix.D.to_array x in
let x = Owl_stats.normlise_pdf x in
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let fill = _get_fill spec false in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
plwidth 1.;
pllsty 1;
plpsty 0;
_draw_arc fill 1000. x;
plscol0 1 r' g' b'; plcol0 1;
plwidth old_pensize;
pllsty 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let loglog ?(h=_default_handle) ?(spec=[]) ?x y =
let open Plplot in
let y = Owl_dense_matrix.D.to_array y in
let n = Array.length y in
let x = match x with
| Some mtx -> Owl_dense_matrix.D.to_array mtx
| None ->
Owl_dense_matrix.D.linspace 1. (float_of_int n) n
|> Owl_dense_matrix.D.to_array
in
let axis = _get_axis spec XY in
let x, y = match axis with
| X -> (Array.map Owl_maths.log10 x, y)
| Y -> (x, Array.map Owl_maths.log10 y)
| _ -> (Array.map Owl_maths.log10 x, Array.map Owl_maths.log10 y)
in
_adjust_range h x X;
_adjust_range h y Y;
let p = h.pages.(h.current_page) in
let _ = match axis with
| X -> p.xlogscale <- true
| Y -> p.ylogscale <- true
| _ -> (p.xlogscale <- true; p.ylogscale <- true)
in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "" in
let marker_size = _get_marker_size spec 4. in
let line_style = _get_line_style spec 1 in
let line_width = _get_line_width spec (-1.) in
let old_pensize = h.pensize in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b; plcol0 1;
if line_width > (-1.) then plwidth line_width;
let c' = plgchr () |> fst in
plschr marker_size 1.;
if line_style > 0 && line_style < 9 then
pllsty line_style; plline x y;
if marker <> "" then (
let x', y' = _thinning x, _thinning y in
plstring x' y' marker
);
plschr c' 1.;
plwidth old_pensize;
pllsty 1;
plscol0 1 r' g' b'; plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p LINE line_style color marker color 0 color;
if not h.holdon then output h
let semilogx ?(h=_default_handle) ?(spec=[]) ?x y =
let spec = spec @ [Axis X] in
match x with
| Some arr -> loglog ~h ~spec ~x:arr y
| None -> loglog ~h ~spec y
let semilogy ?(h=_default_handle) ?(spec=[]) ?x y =
let spec = spec @ [Axis Y] in
match x with
| Some arr -> loglog ~h ~spec ~x:arr y
| None -> loglog ~h ~spec y
let surf ?(h=_default_handle) ?(spec=[]) x y z =
let open Plplot in
let x = Owl_dense_matrix.D.(row x 0 |> to_array) in
let y = Owl_dense_matrix.D.(col y 0 |> to_array) in
let z = Owl_dense_matrix.D.transpose z in
let z0 = Owl_dense_matrix.D.to_arrays z in
let z1 = Owl_dense_matrix.D.to_array z in
_adjust_range h x X;
_adjust_range h y Y;
_adjust_range h z1 Z;
let zmin, zmax = Owl_stats.minmax z1 in
let clvl = Owl_dense_matrix.D.(linspace zmin zmax 10 |> to_array) in
let p = h.pages.(h.current_page) in
p.is_3d <- true;
p.altitude <- _get_altitude spec p.altitude;
p.azimuth <- _get_azimuth spec p.azimuth;
let mag_color = _get_mag_color spec true in
let contour = _get_contour spec false in
let curtain = _get_curtain spec false in
let faceted = _get_faceted spec false in
let opt = [ PL_DIFFUSE ] in
let opt = opt @ if mag_color then [ PL_MAG_COLOR ] else [] in
let opt = opt @ if contour then [ PL_BASE_CONT; PL_SURF_CONT ] else [] in
let opt = opt @ if curtain then [ PL_DRAW_SIDES ] else [] in
let opt = opt @ if faceted then [ PL_FACETED ] else [] in
let f = (fun () ->
plsurf3d x y z0 opt clvl;
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let plot3d = surf
let mesh ?(h=_default_handle) ?(spec=[]) x y z =
let open Plplot in
let x = Owl_dense_matrix.D.(row x 0 |> to_array) in
let y = Owl_dense_matrix.D.(col y 0 |> to_array) in
let z = Owl_dense_matrix.D.transpose z in
let z0 = Owl_dense_matrix.D.to_arrays z in
let z1 = Owl_dense_matrix.D.to_array z in
_adjust_range h x X;
_adjust_range h y Y;
_adjust_range h z1 Z;
let zmin, zmax = Owl_stats.minmax z1 in
let clvl = Owl_dense_matrix.D.(linspace zmin zmax 10 |> to_array) in
let p = h.pages.(h.current_page) in
p.is_3d <- true;
p.altitude <- _get_altitude spec p.altitude;
p.azimuth <- _get_azimuth spec p.azimuth;
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let mag_color = _get_mag_color spec true in
let contour = _get_contour spec false in
let curtain = _get_curtain spec false in
let opt = [ PL_MESH ] in
let opt = opt @ [ _get_zline spec PL_DRAW_LINEXY ] in
let opt = opt @ if mag_color then [ PL_MAG_COLOR ] else [] in
let opt = opt @ if contour then [ PL_BASE_CONT; PL_SURF_CONT ] else [] in
let opt = opt @ if curtain then [ PL_DRAW_SIDES ] else [] in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
match contour with
| true -> plmeshc x y z0 opt clvl
| false -> plmesh x y z0 opt
;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let heatmap ?(h=_default_handle) x y z =
let open Plplot in
let x = Owl_dense_matrix.D.(row x 0 |> to_array) in
let y = Owl_dense_matrix.D.(col y 0 |> to_array) in
let z = Owl_dense_matrix.D.transpose z in
let z0 = Owl_dense_matrix.D.to_arrays z in
let z1 = Owl_dense_matrix.D.to_array z in
_adjust_range h x X;
_adjust_range h y Y;
_adjust_range h z1 Z;
let xmin, xmax = Owl_stats.minmax x in
let ymin, ymax = Owl_stats.minmax y in
let zmin, zmax = Owl_stats.minmax z1 in
let clvl = Owl_dense_matrix.D.(linspace zmin zmax 10 |> to_array) in
let p = h.pages.(h.current_page) in
let f = (fun () ->
plshades z0 xmin xmax ymin ymax clvl 1.0 0 1.0 false
) in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let contour ?(h=_default_handle) x y z =
let open Plplot in
let m, n = Owl_dense_matrix.D.shape z in
let x0 = Owl_dense_matrix.D.to_arrays x in
let x1 = Owl_dense_matrix.D.to_array x in
let y0 = Owl_dense_matrix.D.to_arrays y in
let y1 = Owl_dense_matrix.D.to_array y in
let z0 = Owl_dense_matrix.D.to_arrays z in
let z1 = Owl_dense_matrix.D.to_array z in
_adjust_range h x1 X;
_adjust_range h y1 Y;
let zmin, zmax = Owl_stats.minmax z1 in
let clvl = Owl_dense_matrix.D.(linspace zmin zmax 10 |> to_array) in
let p = h.pages.(h.current_page) in
let f = (fun () ->
plset_pltr (pltr2 x0 y0);
plcont z0 1 m 1 n clvl;
plunset_pltr ()
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let _draw_extended_line x0 y0 x1 y1 l r u d =
let open Plplot in
let x0, y0, x1, y1 = if x0 > x1 then x1, y1, x0, y0 else x0, y0, x1, y1 in
let yl = if x0 = x1 then u else y0 -. (y1 -. y0) /. (x1 -. x0) *. (x0 -. l) in
let yr = if x0 = x1 then d else y1 +. (y1 -. y0) /. (x1 -. x0) *. (r -. x1) in
let xl = if x0 = x1 then x0 else l in
let xr = if x0 = x1 then x0 else r in
pllsty 1; pljoin x0 y0 x1 y1;
pllsty 3; pljoin xl yl x0 y0;
pllsty 3; pljoin x1 y1 xr yr;
pllsty 1
let probplot ?(h=_default_handle) ?(spec=[]) ?(dist=(Owl_stats.gaussian_ppf ~mu:0. ~sigma:1.)) ?(noref=false) x =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x |> Owl_stats.sort ~inc:true in
let y =
let n = Array.length x in
let qth = Owl_dense_matrix.D.linspace ((1. -. 0.5) /. float_of_int n)
(( float_of_int n -. 0.5) /. float_of_int n) n in
let q = Owl_dense_matrix.D.map dist qth in
Owl_dense_matrix.D.to_array q
in
_adjust_range h x X;
_adjust_range h y Y;
let p1y, p1x = (Owl_stats.first_quartile y, Owl_stats.first_quartile x) in
let p3y, p3x = (Owl_stats.third_quartile y, Owl_stats.third_quartile x) in
let left, right = Owl_stats.minmax x in
let up, down = Owl_stats.minmax y in
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "#[0x002b]" in
let marker_size = _get_marker_size spec 4. in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
let c' = plgchr () |> fst in
plschr marker_size 1.;
if not noref then _draw_extended_line p1x p1y p3x p3y left right up down;
plstring x y marker;
plschr c' 1.;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p SCATTER 0 color marker color 0 color;
if not h.holdon then output h
let normplot ?(h=_default_handle) ?(spec=[]) ?(sigma=1.) x =
let dist = Owl_stats.gaussian_ppf ~mu:0. ~sigma in
probplot ~h ~spec ~dist:dist x
let wblplot ?(h=_default_handle) ?(spec=[]) ?(lambda=1.) ?(k=1.) x =
let open Plplot in
let x = Owl_dense_matrix.D.to_array x |> Owl_stats.sort ~inc:true in
let dist = Owl_stats.weibull_ppf ~shape:k ~scale:lambda in
let y =
let n = Array.length x in
let qth = Owl_dense_matrix.D.linspace ((1. -. 0.5) /. float_of_int n)
(( float_of_int n-. 0.5) /. float_of_int n) n in
let q = Owl_dense_matrix.D.map dist qth in
Owl_dense_matrix.D.to_array q
in
_adjust_range h x X;
_adjust_range h y Y;
let p1y, p1x = (Owl_stats.first_quartile y, Owl_stats.first_quartile x) in
let p3y, p3x = (Owl_stats.third_quartile y, Owl_stats.third_quartile x) in
let left, right = Owl_stats.minmax x in
let up, down = Owl_stats.minmax y in
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "#[0x002b]" in
let marker_size = _get_marker_size spec 4. in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
let c' = plgchr () |> fst in
plschr marker_size 1.;
_draw_extended_line p1x p1y p3x p3y left right up down;
plstring x y marker;
plschr c' 1.;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p SCATTER 0 color marker color 0 color;
if not h.holdon then output h
let _ecdf_dist a b p =
let rec _find_rec x lst i = match lst with
| hd::tl -> if (hd > x) then i - 1 else _find_rec x tl (i+1)
| _ -> (Array.length b) - 1
in
let ticks = Array.to_list b in
let i = _find_rec p ticks 1 in
a.(i)
let qqplot ?(h=_default_handle) ?(spec=[]) ?(pd=Owl_stats.gaussian_ppf ~mu:0. ~sigma:1.) ?x y =
let open Plplot in
let y = Owl_dense_matrix.D.to_array y |> Owl_stats.sort ~inc:true in
let n = Array.length y in
let dist = match x with
| Some arr ->
let x = Owl_dense_matrix.D.to_array arr in
let a, b = Owl_stats.ecdf x in
(fun p -> _ecdf_dist a b p)
| None -> pd
in
let qth = Owl_dense_matrix.D.linspace ((1. -. 0.5) /. float_of_int n)
(( float_of_int n-. 0.5) /. float_of_int n) n in
let q = Owl_dense_matrix.D.map dist qth in
let x = Owl_dense_matrix.D.to_array q in
_adjust_range h x X;
_adjust_range h y Y;
let p1y, p1x = (Owl_stats.first_quartile y, Owl_stats.first_quartile x) in
let p3y, p3x = (Owl_stats.third_quartile y, Owl_stats.third_quartile x) in
let left, right = Owl_stats.minmax x in
let up, down = Owl_stats.minmax y in
let p = h.pages.(h.current_page) in
let color = _get_rgb spec p.fgcolor in
let r, g, b = color in
let marker = _get_marker spec "#[0x002b]" in
let marker_size = _get_marker_size spec 4. in
let f = (fun () ->
let r', g', b' = plgcol0 1 in
plscol0 1 r g b;
plcol0 1;
let c' = plgchr () |> fst in
plschr marker_size 1.;
_draw_extended_line p1x p1y p3x p3y left right up down;
plstring x y marker;
plschr c' 1.;
plscol0 1 r' g' b';
plcol0 1
)
in
p.plots <- Array.append p.plots [|f|];
_add_legend_item p SCATTER 0 color marker color 0 color;
if not h.holdon then output h
let scatterhist = None [@@warning "-32"]
let image ?(h=_default_handle) x =
let open Plplot in
let x = Owl_dense_matrix.D.rotate x 90 in
let width, height = Owl_dense_matrix.D.shape x in
let num_col = Owl_dense_matrix.D.max' x in
let img = Owl_dense_matrix.D.to_arrays x in
let width = float_of_int width in
let height = float_of_int height in
let num_col = int_of_float num_col in
let x = [|1.0; width|] in
let y = [|1.0; height|] in
_adjust_range h x X;
_adjust_range h y Y;
h.page_size <- (int_of_float width, int_of_float height);
let p = h.pages.(h.current_page) in
let _ = p.no_axes <- true in
let f = (fun () ->
let r = [|0.0; 1.0|] in
let g = [|0.0; 1.0|] in
let b = [|0.0; 1.0|] in
let pos = [|0.0; 1.0|] in
plscmap1n num_col;
plscmap1l true pos r g b None;
plimage img 1.0 width 1.0 height 0.0 0.0 1.0 width 1.0 height;
)
in
p.plots <- Array.append p.plots [|f|];
if not h.holdon then output h
let spy ?(h=_default_handle) ?(spec=[]) x =
let xs = Owl_utils.Stack.make () in
let ys = Owl_utils.Stack.make () in
let m, n = Owl_dense_matrix.D.shape x in
for i = 0 to m - 1 do
for j = 0 to n - 1 do
let a = Owl_dense_matrix.D.get x i j in
if a <> 0. then (
Owl_utils.Stack.push xs (float_of_int i);
Owl_utils.Stack.push ys (float_of_int j);
)
done;
done;
let x = Owl_utils.Stack.to_array xs in
let y = Owl_utils.Stack.to_array ys in
let x = Owl_dense_matrix.D.of_arrays [|x|] in
let y = Owl_dense_matrix.D.of_arrays [|y|] in
scatter ~h ~spec x y