-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdemo.lisp
More file actions
287 lines (279 loc) · 11.6 KB
/
demo.lisp
File metadata and controls
287 lines (279 loc) · 11.6 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
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
;;;; demo.lisp
#|
This file contains a demonstration of some of the functions of vgplot.
vgplot is an interface to the gnuplot utility.
Copyright (C) 2013 - 2020 Volker Sarodnick
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
|#
(in-package #:vgplot)
(defun drop-substring (substring instring)
(let ((i 0)
(ilen (length substring))
(chars))
(loop for c across instring do
(if (char= c (aref substring i))
(incf i)
(if (= i 0)
(push c chars)
(progn
(loop for j below i do
(push (aref substring j) chars))
(push c chars)
(setf i 0))))
(if (= i ilen)
(setf i 0)))
(coerce (nreverse chars) 'string)))
(defun print-n-run-list (lst)
"Print commands in lst and run them after a (read-char),
ENTER continue, all other characters break and quit demo"
(loop for cmd in lst do
(progn (princ (drop-substring "vgplot::" (format nil "~s" cmd)))
(unless (char= #\Newline (read-char))
(close-all-plots)
(return-from print-n-run-list nil))
(eval cmd))))
(defun demo ()
"Show usecases of vgplot."
(let ((*print-case* :downcase))
(format t "****************************************************************~%")
(format t "vgplot demo, run commands by pressing RETURN~%")
(format t "press q (or any other character) RETURN to leave the demo~%")
(format t "****************************************************************~%")
(print-n-run-list
'( ;; add demo commands here
(progn
(plot #(1 2 3) #(0 -2 -17) ";silly example;")
(title "Simple curve")
(text 1.2 -14 "Plot vectors with legend and add a title"))
(progn
(defvar x)
(defvar y)
(setf x (range 0 (* 2 pi) 0.01))
(setf y (map 'vector #'sin x))
(plot x y "y = sin(x)")
(xlabel "[rad]")
(ylabel "magnitude")
(text 0.2 -0.6 "Use function range to create vectors and add labels to axes"))
(and "text-show-labels shows the active labels and their tags"
(text-show-label))
(and "Usefull to delete or change labels"
(text-delete 1 2))
(text 0.5 -0.5 "You can use a different size" :fontsize 14)
(progn
(text 0.5 -0.5 "You can change a present label"
:tag 1 :rotation 60 :font "Times" :fontsize 12 :color "green")
(and "font definitions for text change also the font of the legend,
the following is needed to change the keys back")
(format-plot t "set key font \",10\"")
(replot))
(and "the :color color command can also send arbitrarily style commands"
(text 0.5 -0.5 "Label in the same color as line 1" :tag 1 :color "tc lt 1"))
(progn
(text-delete 1)
(text 3 0.5 "axis can change the range of the x axis")
(axis (list (/ pi 2) 5)))
(progn
(text -0.5 -0.5 "axis can also change the range of both x and y axis" :tag 1)
(axis (list -1 pi -1.2 1.2)))
(progn
(text 0.5 -0.5 "t means autoscale and nil unchanged corresponding axis" :tag 1)
(axis '(t nil)))
(progn
(text 0.5 -0.5 "another example of the use of axis" :tag 1)
(axis '(nil nil -1.5 t)))
(progn
(text 0.5 -0.5 "Remove the grid" :tag 1)
(grid nil))
(progn
(text 0.5 -0.5 "print-plot prints the plot into a file" :tag 1)
(print-plot #p"plot.pdf"))
(and "close-plot closes the actual plot"
(close-plot))
(progn
(defvar z)
(setf z (map 'vector #'cos x))
(plot x y "b;y = sin(x);" x z "g;y = cos(x);")
(title "Some Other Graphs"))
(progn
(legend :outside :boxon :southeast :right)
(title "Use legend to manipulate the legend (aka keys)"))
(progn
(legend :at 5 0.4)
(title "Place the legend directly at position x y"))
(progn
(new-plot)
(setf y (map 'vector #'(lambda (a) (sin (* 2 a))) x))
(plot x y "+k;y = cos(2x) (new-plot);")
(text 0.5 -0.5 "new-plot adds a new plot window"))
(text-show-label)
(progn
(plot x y "og;y = cos(2x) (new-plot);" #(0 1 2 4 7) #(-1 2 1 -1.2 0) ":r;dotted style;")
(text 0.5 0.5 "Different line styles" :tag 1))
(progn
(plot x y "#bf0810;# followed by 6 hex digits, e.g. #bf0810;")
(text 0.5 0.5 "Use of RGB colors in line style" :tag 1))
(progn
(and "(format-plot) allows direct commands to the running gnuplot process")
(format-plot t "set size square 0.5,0.5~%")
(replot))
(close-all-plots)
(progn
(plot x y "w;" #(0 1 2 4 7) #(-1 2 1 -1.2 0) "y;")
(format-plot t "set object rectangle from screen 0,0 to screen 1,1 fillcolor 'black' behind")
(format-plot t "set border lc 'white'")
(text 2.5 1.5 "Another example for use of format-plot commands" :color "white")
(vgplot:replot))
(close-all-plots)
(plot #(1 2 3 4) #(2 4 3 1) ";use of additional styles;with linespoints pt 7 ps 2 lc 'red'")
(close-all-plots)
(let ((x #(1 2 3 4)))
(subplot 3 2 0)
(plot x #(0.5 2 -3 4) x #(-1 2 3 4))
(title "Use of multiplots")
(subplot 3 2 1)
(plot x #(0.2 0.4 3 0.4) x #(-1 -2 3 4))
(title "")
(subplot 3 2 2)
(plot x #(-1 2 3 4) x #(-1 -2 -3 4))
(subplot 3 2 3)
(plot x #(1 -1.8 -2.8 4) x #(-1 -2 -3 -4))
(subplot 3 2 4)
(plot x #(1 2 -3 4) x #(1 -2 3 4))
(subplot 3 2 5)
(plot x #(1 -2 3 4) x #(1 -2 -3 4)))
(let ((x #(1 10 100 1000))
(y #(1 2 3 4)))
(subplot 2 2 0)
(plot x y)
(title "Linear axes (plot)")
(subplot 2 2 1)
(semilogx x y)
(title "Log x axis (semilogx)")
(subplot 2 2 2)
(semilogy x y)
(title "Log y axis (semilogy)")
(subplot 2 2 3)
(loglog x y)
(title "Log x and y axis (loglog)"))
(close-plot)
(let* ((z (range 0 (* 3 pi) 0.1))
(x (map 'vector #'sin z))
(y (map 'vector #'cos z)))
(3d-plot x y z)
(title "Example of a 3D plot"))
(close-plot)
(let ((zz (make-array (list 3 4) :initial-contents '((0.8 1.5 1.7 2.8) (1.8 1.2 1.2 2.1) (1.7 1.0 1.0 1.9)))))
(vgplot:surf zz "r;array plotted without providing xx or yy;"))
(close-plot)
(let* ((eps double-float-epsilon)
(fun #'(lambda (x y) (/ (sin (sqrt (+ (* x x) (* y y) eps))) (sqrt (+ (* x x) (* y y) eps)))))
(x (range -8 8 0.2))
(y (range -8 8 0.2))
(xx (meshgrid-x x y))
(yy (meshgrid-y x y))
(zz (meshgrid-map fun xx yy)))
(surf xx yy zz)
(format-plot nil "set hidden3d")
(format-plot nil "set pm3d")
(format-plot nil "set arrow 1 from -5,-2.1,1.4 to 0,0,1")
(format-plot nil "set label 1 'This is equal to (0,0,1)' at -5,-2,1.5 centre")
(xlabel "x")
(ylabel "y")
(zlabel "z")
(title "Example of a surface plot (the famous sombrero)"))
(close-plot)
(progn
(setf y #(0 4 6.5 6.8 6.5 6.2 6.1 6.05 6.0 6.0))
(stairs y)
(title "Example of a stairstep plot"))
(progn
(setf x (range (length y)))
(stairs x y "line 1" '(0 1 10) '(0 6 6) "k;line 2;"))
(flet ((e-fun (x) (- 1 (exp (- x)))))
(let* ((x (range 0 6 0.1))
(y (map 'vector #'e-fun x))
(xd (range 7))
(yd (map 'vector #'e-fun xd))
(sampled (stairs-no-plot xd yd)))
(plot x y "b;continuous;"
(first sampled) (second sampled) "sampled"))
(legend :southeast)
(title "Example of a mixture of continuous and discrete characteristics"))
(progn
(bar :y '(((0.9 0.8 0.3))
((0.6 0.7 0.1))))
(title "Very simple example of a bar plot"))
(progn
(bar :x #(2011 2013 2014)
:y '((#(0.9 0.8 0.3) :label "Values 1")
(#(0.6 0.7 0.1) :label "Values 2")))
(title "Simple example of a bar plot"))
(progn
(bar :x #("January 2015" "Mars 2015" "June 2015")
:y '((#(0.8 0.9 -0.3) :color "blue" :label "Values 1")
(#(0.7 0.65 0.5) :color "red" :label "Values 2")
(#(0.75 0.4 0.1) :color "cyan" :label "Values 3")
(#(0.6 0.5 0.2) :color "green" :label "Values 4"))
:width 0.5)
(axis '(t t -0.5 1))
(xlabel "Year of the event")
(ylabel "Result")
(grid nil)
(title "Another example of a bar plot"))
(progn
(bar :x #("January 2015" "Mars 2015" "June 2015")
:y '((#(0.8 0.9 -0.3) :color "blue" :label "Values 1")
(#(0.7 0.65 0.5) :color "red" :label "Values 2")
(#(0.75 0.4 0.1) :color "cyan" :label "Values 3")
(#(0.6 0.5 0.2) :color "green" :label "Values 4"))
:width 0.5
:gap 4)
(axis '(t t -0.5 1))
(xlabel "Year of the event")
(ylabel "Result")
(grid nil)
(title "Use of the gap parameter in a grouped bar plot (gap = 4.0)"))
(progn
(bar :x #("January 2015" "Mars 2015" "June 2015")
:y '((#(0.8 0.9 -0.3) :color "blue" :label "Values 1")
(#(0.7 0.65 0.5) :color "red" :label "Values 2")
(#(0.75 0.4 0.1) :color "cyan" :label "Values 3")
(#(0.6 0.5 0.2) :color "green" :label "Values 4"))
:width 1.2)
(axis '(t t -0.5 1))
(xlabel "Year of the event")
(ylabel "Result")
(grid nil)
(title "Use of the width parameter in a grouped bar plot (width = 1.2)"))
(progn
(bar :x #("John" "Paul" "Mary")
:y '((#(0.8 0.9 0.3) :color "blue" :label "Values 1")
(#(0.7 0.65 0.5) :color "red" :label "Values 2")
(#(0.75 0.4 0.1) :color "cyan" :label "Values 3")
(#(0.6 0.5 0.2) :color "green" :label "Values 4"))
:width 0.5
:style "stacked")
(axis '(t t 0 3))
(xlabel "Student")
(ylabel "Result")
(title "Example of a stacked bar plot"))
(close-plot)
(or "The following works if you copy data.txt and data.csv
from vgplot's source directory to your directory")
(when (cl-fad:file-exists-p "data.txt")
(plot-file "data.txt"))
(when (cl-fad:file-exists-p "data.csv")
(plot-file "data.csv"))
(when (cl-fad:file-exists-p "data.csv")
(plot (first (load-data-file "data.csv")))
(text 2 -1 "load-data-file returns data from a csv file"))
(close-all-plots)))))