-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplotter-histogram.lisp
More file actions
72 lines (66 loc) · 2.31 KB
/
plotter-histogram.lisp
File metadata and controls
72 lines (66 loc) · 2.31 KB
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
(in-package :plotter)
;; ---------------------------------------------------------
;; org can be a list of (type xorg yorg), e.g., '(:frac 0.9 0.96)
;; or a pair of typed values ((type xorg) (type yorg)), e.g., '((:frac 0.9) (:data 14.3))
;;
;; convert to a list of typed pairs, e.g., '((:frac 0.9) (:data 14.3))
;;
(defun get-xy-orgs (org)
(if (= 3 (length org))
(list (list (first org) (second org))
(list (first org) (third org)))
org))
(defun draw-text (pane str org &rest args)
(destructuring-bind (xorg yorg) (get-xy-orgs org)
(apply #'outsxy pane xorg yorg str (append args *default-args*))))
;; ------------------------------------------
(defun do-plot-histogram (pane v &rest args
&key min max range nbins binwidth
ylog cum (norm t)
(line-type :histo)
&allow-other-keys)
(multiple-value-bind (x h bw)
(vm:histogram v
:min min
:max max
:range range
:nbins nbins
:binwidth binwidth)
(let* (;; (nel (array-total-size v))
;; (tot (* nel bw))
(tot (length v))
(nel (length v))
minnz)
(when norm
(loop for v across h
for ix from 0
do
(setf (aref h ix) (/ v tot bw))
))
(when cum
(loop for vy across h
for ix from 0
for sf = (if norm bw (/ nel))
for sum = (* sf vy) then (+ sum (* sf vy))
do
(setf (aref h ix) sum)
(unless (or minnz
(zerop sum))
(setf minnz sum))
))
(when ylog
(let ((zlim (cond (cum minnz)
(norm (/ 0.9 tot))
(t 0.9)
)))
(loop for v across h
for ix from 0
do
(when (zerop v)
(setf (aref h ix) zlim)))
))
(apply #'plot pane x h :line-type line-type args)
)))
;; user callable routine
(defun histogram (pane v &rest args)
(apply #'do-plot-histogram pane (coerce v 'vector) (append args *default-args*)))