descriptive.mac: declare local variable to avoid possibility of name collision.
[maxima.git] / src / gnuplot_def.lisp
blob80c174bd5fdaae521ece202c6e1cf871bc8d972d
1 ;; gnuplot_def.lisp: routines for Maxima's interface to gnuplot
2 ;; Copyright (C) 2007-2021 J. Villate
3 ;; Time-stamp: "2022-03-28 11:45:15 villate"
4 ;;
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License
7 ;; as published by the Free Software Foundation; either version 2
8 ;; of the License, or (at your option) any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 ;; MA 02110-1301, USA
20 (in-package :maxima)
22 ;; Checks that color is a six-digit hexadecimal number with the prefix #,
23 ;; or a symbol for one of the 12 pre-defined colors, in which case the
24 ;; hexadecimal code for that color will be returned. Unknown colors are
25 ;; converted into black.
26 (defun rgb-color (color)
27 (if (plotcolorp color)
28 (case color
29 ($red "#ff0000")
30 ($green "#00ff00")
31 ($blue "#0000ff")
32 ($magenta "#ff00ff")
33 ($cyan "#00ffff")
34 ($yellow "#ffff00")
35 ($orange "#ffa500")
36 ($violet "#ee82ee")
37 ($brown "#a52a2a")
38 ($gray "#bebebe")
39 ($black "#000000")
40 ($white "#ffffff")
41 (t color))
42 "#000000"))
44 ;; Given a list of valid colors (see rgb-color function) and an object c
45 ;; that can be a real number or a string, produces a gnuplot color
46 ;; specification for c; when c is real, its nearest integer is assigned
47 ;; to one of the numbers in the list, using modulo length of the list.
48 (defun gnuplot-color (colors c)
49 (unless (listp colors) (setq colors (list colors)))
50 (when (realp c)
51 (unless (integerp c) (setq c (round c)))
52 (setq c (nth (mod (1- c) (length colors)) colors)))
53 (format nil "rgb ~s" (rgb-color c)))
55 (defun gnuplot-pointtype (type)
56 (case type
57 ($bullet 7) ($circle 6) ($plus 1) ($times 2) ($asterisk 3) ($box 5)
58 ($square 4) ($triangle 9) ($delta 8) ($wedge 11) ($nabla 10)
59 ($diamond 13) ($lozenge 12) (t 7)))
61 (defun gnuplot-pointtypes (types n)
62 (unless (integerp n) (setq n (round n)))
63 (unless (listp types) (setq types (list types)))
64 (gnuplot-pointtype (nth (mod (- n 1) (length types)) types)))
66 ;; style is a list starting with one of the symbols: points, lines,
67 ;; linespoints or dots,
68 ;; The meaning of the numbers that follow the symbol are:
70 ;; lines, linewidth, color
71 ;; points, radius, color, pointtype
72 ;; linespoints, linewidth, radius, color, pointtype
73 ;; dots, color
75 ;; linewidth and radius are measured in the same units and can be
76 ;; floating-point numbers.
78 ;; type must be an integer
79 ;; color can be an integer, used as index to get one of the colors defined
80 ;; by the color option, or a 6-digit hexadecimal number #rrggbb
82 (defun gnuplot-curve-style (style colors types i)
83 (unless (listp style) (setq style (list style)))
84 (unless (listp colors) (setq colors (list colors)))
85 (with-output-to-string
86 (st)
87 (case (first style)
88 ($dots
89 (format st "with dots")
90 (if (second style)
91 (format st " lt ~d" (gnuplot-color colors (second style)))
92 (format st " lt ~d" (gnuplot-color colors i))))
93 ($impulses
94 (format st "with impulses")
95 (if (integerp (second style))
96 (format st " lt ~d" (gnuplot-color colors (second style)))
97 (format st " lt ~d" (gnuplot-color colors i))))
98 ($lines
99 (format st "with lines")
100 (if (realp (second style))
101 (format st " lw ~f" (second style)))
102 (if (third style)
103 (format st " lt ~d" (gnuplot-color colors (third style)))
104 (format st " lt ~d" (gnuplot-color colors i))))
105 ($points
106 (format st "with points")
107 (if (realp (second style))
108 (format st " ps ~f" (/ (second style) 2))
109 (format st " ps 1.5"))
110 (if (third style)
111 (format st " lt ~d" (gnuplot-color colors (third style)))
112 (format st " lt ~d" (gnuplot-color colors i)))
113 (if (integerp (fourth style))
114 (format st " pt ~d" (gnuplot-pointtypes types (fourth style)))
115 (format st " pt ~d" (gnuplot-pointtypes types i))))
116 ($linespoints
117 (format st "with linespoints")
118 (if (realp (second style))
119 (format st " lw ~f" (second style)))
120 (if (realp (third style))
121 (format st " ps ~f" (/ (third style) 2))
122 (format st " ps 1.5"))
123 (if (fourth style)
124 (format st " lt ~d" (gnuplot-color colors (fourth style)))
125 (format st " lt ~d" (gnuplot-color colors i)))
126 (if (integerp (fifth style))
127 (format st " pt ~d" (gnuplot-pointtypes types (fifth style)))
128 (format st " pt ~d" (gnuplot-pointtypes types i))))
129 (t (format st "with lines lt ~d" (gnuplot-color colors i))))))
131 (defun gnuplot-palette (palette)
132 ;; palette should be a list starting with one of the symbols: hue,
133 ;; saturation, value, gray or gradient.
135 ;; If the symbol is gray, it should be followed by two floating point
136 ;; numbers that indicate the initial gray level and the interval of
137 ;; gray values.
139 ;; If the symbol is one of hue, saturation or value, it must be followed
140 ;; by three numbers that specify the hue, saturation and value for the
141 ;; initial color, and a fourth number that gives the range of values for
142 ;; the increment of hue, saturation or value.
143 ;; The values for the initial hue, saturation, value and grayness should
144 ;; be within 0 and 1, while the range can be higher or even negative.
146 ;; If the symbol is gradient, it must be followed by either a list of valid
147 ;; colors or by a list of lists with two elements, a number and a valid color.
149 (unless (listp palette) (setq palette (list palette)))
150 (let (hue sat val gray range fun)
151 (case (first palette)
152 ($gray
153 (case (length (rest palette))
154 (2 (setq gray (second palette)) (setq range (third palette)))
155 (t (merror
156 (intl:gettext
157 "palette: gray must be followed by two numbers."))))
158 (when (or (< gray 0) (> gray 1))
159 (setq gray (- gray (floor gray)))))
160 (($hue $saturation $value)
161 (case (length (rest palette))
162 (4 (setq hue (second palette))
163 (setq sat (third palette))
164 (setq val (fourth palette))
165 (setq range (fifth palette)))
166 (t (merror
167 (intl:gettext
168 "palette: ~M must be followed by four numbers.")
169 (first palette))))
170 (when (or (< hue 0) (> hue 1)) (setq hue (- hue (floor hue))))
171 (when (or (< sat 0) (> sat 1)) (setq sat (- sat (floor sat))))
172 (when (or (< val 0) (> val 1)) (setq val (- val (floor val))))))
173 (with-output-to-string (st)
174 (case (first palette)
175 ($hue
176 (if (or (< (+ hue range) 0) (> (+ hue range) 1))
177 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
178 hue range hue range))
179 (setq fun (format nil "~,3f+~,3f*gray" hue range)))
180 (format st "model HSV functions ~a, ~,3f, ~,3f" fun sat val))
181 ($saturation
182 (if (or (< (+ sat range) 0) (> (+ sat range) 1))
183 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
184 sat range sat range))
185 (setq fun (format nil "~,3f+~,3f*gray" sat range)))
186 (format st "model HSV functions ~,3f, ~a, ~,3f" hue fun val))
187 ($value
188 (if (or (< (+ val range) 0) (> (+ val range) 1))
189 (setq fun (format nil "~,3f+~,3f*gray" val range))
190 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
191 val range val range)))
192 (format st "model HSV functions ~,3f, ~,3f, ~a" hue sat fun))
193 ($gray
194 (if (or (< (+ gray range) 0) (> (+ gray range) 1))
195 (setq fun (format nil "~,3f+~,3f*gray" gray range))
196 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
197 gray range gray range)))
198 (format st "model RGB functions ~a, ~a, ~a" fun fun fun))
200 ($gradient
201 (let* ((colors (rest palette)) (n (length colors)) (map nil))
202 ;; map is constructed as (n1 c1 n2 c2 ... nj cj) where ni is a
203 ;; decreasing sequence of numbers (n1=1, nj=0) and ci are colors
204 (cond
205 ;; Maxima list of numbers and colors (((mlist) ni ci) ...)
206 ((listp (first colors))
207 (setq colors (sort colors #'< :key #'cadr))
208 (dotimes (i n)
209 (setq map (cons (rgb-color (third (nth i colors))) ;; color
210 (cons
211 (/ (- (second (nth i colors)) ;; ni minus
212 (second (first colors))) ;; smallest ni
213 (- (second (nth (- n 1) colors));; biggest
214 (second (first colors)))) ;; - smallest
215 map)))))
216 ;; list of only colors
217 (t (dotimes (i n)
218 (setq map (cons (rgb-color (nth i colors)) ;; color i
219 (cons (/ i (1- n)) map)))))) ;; number i
221 ;; prints map with the format: nj, "cj", ...,n1, "c1"
222 (setq fun (format nil "~{~f ~s~^, ~}" (reverse map)))
223 ;; outputs the string: defined (nj, "cj", ...,n1, "c1")
224 (format st "defined (~a)" fun)))
226 (merror
227 (intl:gettext
228 "palette: wrong keyword ~M. Must be hue, saturation, value, gray or gradient.")
229 (first palette)))))))
231 (defun gnuplot-plot3d-command (file palette gstyles colors titles n)
232 (let (title (style "with pm3d"))
233 (with-output-to-string (out)
234 (format out "splot ")
235 (do ((i 1 (+ i 1))) ((> i n) (format out "~%"))
236 (unless palette
237 (if gstyles
238 (setq style (ensure-string (nth (mod i (length gstyles)) gstyles)))
239 (setq style
240 (format nil "with lines lt ~a" (gnuplot-color colors i)))))
241 (when (> i 1) (format out ", "))
242 (if titles
243 (setq title (nth (mod i (length titles)) titles))
244 (setq title ""))
245 (format out "~s title ~s ~a" file title style)))))
247 (defun gnuplot-terminal-and-file (plot-options)
248 (let ((gstrings
249 (if (getf plot-options :gnuplot_strings) "enhanced" "noenhanced"))
250 (gnuplot-svg-background (getf plot-options :gnuplot_svg_background))
251 terminal-command out-file (preserve-file t))
252 (cond
253 ((getf plot-options :svg_file)
254 (if (getf plot-options :gnuplot_svg_term_command)
255 (setq terminal-command
256 (getf plot-options :gnuplot_svg_term_command))
257 (setq terminal-command
258 (format nil "set term svg font \",14\" ~a~@[ background '~a'~]" gstrings gnuplot-svg-background)))
259 (setq out-file (getf plot-options :svg_file)))
260 ((getf plot-options :png_file)
261 (if (getf plot-options :gnuplot_png_term_command)
262 (setq terminal-command
263 (getf plot-options :gnuplot_png_term_command))
264 (setq terminal-command
265 (format nil "set term pngcairo font \",12\" ~a" gstrings)))
266 (setq out-file (getf plot-options :png_file)))
267 ((getf plot-options :pdf_file)
268 (if (getf plot-options :gnuplot_pdf_term_command)
269 (setq terminal-command
270 (getf plot-options :gnuplot_pdf_term_command))
271 (setq terminal-command
272 (format nil "set term pdfcairo color solid lw 3 size 17.2 cm, 12.9 cm font \",18\" ~a" gstrings)))
273 (setq out-file (getf plot-options :pdf_file)))
274 ((getf plot-options :ps_file)
275 (if (getf plot-options :gnuplot_ps_term_command)
276 (setq terminal-command
277 (getf plot-options :gnuplot_ps_term_command))
278 (setq terminal-command
279 (format nil "set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings)))
280 (setq out-file (getf plot-options :ps_file)))
281 ((eq (getf plot-options :gnuplot_term) '$ps)
282 (if (getf plot-options :gnuplot_ps_term_command)
283 (setq terminal-command
284 (getf plot-options :gnuplot_ps_term_command))
285 (setq terminal-command
286 (format nil "set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings)))
287 (if (getf plot-options :gnuplot_out_file)
288 (setq out-file (getf plot-options :gnuplot_out_file))
289 (setq out-file "maxplot.ps")))
290 ((eq (getf plot-options :gnuplot_term) '$dumb)
291 (if (getf plot-options :gnuplot_dumb_term_command)
292 (setq terminal-command
293 (getf plot-options :gnuplot_ps_term_command))
294 (setq terminal-command "set term dumb 79 22"))
295 (if (getf plot-options :gnuplot_out_file)
296 (setq out-file (getf plot-options :gnuplot_out_file))
297 (setq out-file "maxplot.txt")))
298 ((eq (getf plot-options :gnuplot_term) '$default)
299 (if (getf plot-options :gnuplot_default_term_command)
300 (setq terminal-command
301 (getf plot-options :gnuplot_default_term_command))
302 (setq terminal-command
303 (if (getf plot-options :window)
304 (format nil "set term GNUTERM ~d ~a~%"
305 (getf plot-options :window) gstrings)
306 (format nil "set term GNUTERM ~a~%" gstrings)))))
307 ((getf plot-options :gnuplot_term)
308 (setq
309 terminal-command
310 (format nil "set term ~(~a~)"
311 (ensure-string (getf plot-options :gnuplot_term))))
312 (if (getf plot-options :gnuplot_out_file)
313 (setq out-file (getf plot-options :gnuplot_out_file))
314 (setq preserve-file nil
315 out-file
316 (format nil "maxplot.~(~a~)"
317 (get-gnuplot-term (getf plot-options :gnuplot_term)))))))
319 (unless (null out-file) (setq out-file (plot-file-path out-file preserve-file plot-options)))
320 (list terminal-command out-file)))
322 (defmethod plot-preamble ((plot gnuplot-plot) plot-options)
323 (let ((palette (getf plot-options :palette))
324 (meshcolor (if (member :mesh_lines_color plot-options)
325 (getf plot-options :mesh_lines_color)
326 '$black)) terminal-file)
327 (when (find 'mlist palette :key #'car) (setq palette (list palette)))
328 ;; sets-up terminal command and output file name
329 (setq terminal-file (gnuplot-terminal-and-file plot-options))
330 (setf
331 (slot-value plot 'data)
332 (concatenate
333 'string
334 (slot-value plot 'data)
335 (with-output-to-string (dest)
336 ;; reset initial state
337 (format dest "reset~%unset output~%unset multiplot~%")
338 ;; user's preamble
339 (when (and (getf plot-options :gnuplot_preamble)
340 (> (length (getf plot-options :gnuplot_preamble)) 0))
341 (format dest "~a~%" (getf plot-options :gnuplot_preamble)))
342 ;; Don't round numbers with absolute value less than 1e-8 to zero
343 (format dest "set zero 0.0~%")
344 ;; prints terminal and output commands
345 (when (first terminal-file)
346 (format dest "~a~%" (first terminal-file)))
347 (when (second terminal-file)
348 (format dest "set output ~s~%" (second terminal-file)))
349 ;; options specific to plot3d
350 (when (string= (getf plot-options :type) "plot3d")
351 (format dest "set xyplane relative 0~%")
352 (if palette
353 (progn
354 (if meshcolor
355 (progn
356 (format dest
357 "if (GPVAL_VERSION < 5.0) set style line 100 lt rgb ~s lw 1; set pm3d hidden3d 100~%"
358 (rgb-color meshcolor))
359 (format dest
360 "if (GPVAL_VERSION >= 5.0) set pm3d hidden3d 100 border lw 0.5 lt rgb ~s~%"
361 (rgb-color meshcolor))
362 (unless (getf plot-options :gnuplot_4_0)
363 (format dest "set pm3d depthorder~%")))
364 (format dest "set pm3d~%"))
365 (format dest "unset hidden3d~%")
366 (format dest "set palette ~a~%"
367 (gnuplot-palette (rest (first palette)))))
368 (format dest "set hidden3d~%"))
369 (let ((elev (getf plot-options :elevation))
370 (azim (getf plot-options :azimuth)))
371 (when (or elev azim)
372 (if elev
373 (format dest "set view ~d" elev)
374 (format dest "set view "))
375 (when azim (format dest ", ~d" azim))
376 (format dest "~%"))))
377 ;; color_bar can be used by plot3d or plot2d
378 (unless (getf plot-options :color_bar)
379 (format dest "unset colorbox~%"))
380 ;; ----- BEGIN GNUPLOT 4.0 WORK-AROUND -----
381 ;; When the expression plotted is constant, Gnuplot 4.0 fails
382 ;; with a division by 0. Explicitly assigning cbrange prevents
383 ;; the error. Also set zrange to match cbrange.
384 (when (floatp (getf plot-options :const_expr))
385 (format
386 dest "set cbrange [~a : ~a]~%"
387 (1- (getf plot-options :const_expr))
388 (1+ (getf plot-options :const_expr)))
389 (format
390 dest "set zrange [~a : ~a]~%"
391 (1- (getf plot-options :const_expr))
392 (1+ (getf plot-options :const_expr))))
393 ;; ----- END GNUPLOT 4.0 WORK-AROUND -----
394 ;; logarithmic plots
395 (when (getf plot-options :logx) (format dest "set log x~%"))
396 (when (getf plot-options :logy) (format dest "set log y~%"))
397 ;; axes labels and legend
398 (when (getf plot-options :xlabel)
399 (format dest "set xlabel ~s~%" (getf plot-options :xlabel)))
400 (when (getf plot-options :ylabel)
401 (format dest "set ylabel ~s~%" (getf plot-options :ylabel)))
402 (when (getf plot-options :zlabel)
403 (format dest "set zlabel ~s~%" (getf plot-options :zlabel)))
404 (when (and (member :legend plot-options)
405 (null (getf plot-options :legend)))
406 (format dest "unset key~%"))
407 ;; plotting box
408 (when (and (member :box plot-options) (not (getf plot-options :box)))
409 (format dest "unset border~%")
410 (if (and (getf plot-options :axes)
411 (string= (getf plot-options :type) "plot2d"))
412 (format dest "set xtics axis~%set ytics axis~%set ztics axis~%")
413 (format dest "unset xtics~%unset ytics~%unset ztics~%")))
414 ;; 2d grid (specific to plot2d)
415 (when (string= (getf plot-options :type) "plot2d")
416 (format dest "set grid front~%")
417 (if (getf plot-options :grid2d)
418 (format dest "set grid~%")
419 (format dest "unset grid~%"))
420 ;; plot size and aspect ratio for plot2d
421 (if (getf plot-options :same_xy)
422 (format dest "set size ratio -1~%")
423 (if (getf plot-options :yx_ratio)
424 (format dest "set size ratio ~f~%"
425 (getf plot-options :yx_ratio))
426 (if (not (getf plot-options :xy_scale))
427 ;; emit the default only if there is no xy_scale specified.
428 (format dest "set size ratio 0.75~%"))))
429 (if (and (getf plot-options :xy_scale)
430 (listp (getf plot-options :xy_scale)))
431 (format dest "set size ~{~f~^, ~}~%"
432 (getf plot-options :xy_scale))))
433 ;; plot size and aspect ratio for plot3d
434 (when (string= (getf plot-options :type) "plot3d")
435 (when (getf plot-options :same_xy)
436 (format dest "set view equal xy~%"))
437 (when (getf plot-options :same_xyz)
438 (format dest "set view equal xyz~%"))
439 (when (getf plot-options :zmin)
440 (format dest "set xyplane at ~f~%" (getf plot-options :zmin))))
441 ;; axes tics
442 (when (member :xtics plot-options)
443 (let ((xtics (getf plot-options :xtics)))
444 (if (consp xtics)
445 (format dest "set xtics ~{~f~^, ~}~%" xtics)
446 (if xtics
447 (format dest "set xtics ~f~%" xtics)
448 (format dest "unset xtics~%")))))
449 (when (member :ytics plot-options)
450 (let ((ytics (getf plot-options :ytics)))
451 (if (consp ytics)
452 (format dest "set ytics ~{~f~^, ~}~%" ytics)
453 (if ytics
454 (format dest "set ytics ~f~%" ytics)
455 (format dest "unset ytics~%")))))
456 (when (member :ztics plot-options)
457 (let ((ztics (getf plot-options :ztics)))
458 (if (consp ztics)
459 (format dest "set ztics ~{~f~^, ~}~%" ztics)
460 (if ztics
461 (format dest "set ztics ~f~%" ztics)
462 (format dest "unset ztics~%")))))
463 (when (member :color_bar_tics plot-options)
464 (let ((cbtics (getf plot-options :color_bar_tics)))
465 (if (consp cbtics)
466 (format dest "set cbtics ~{~f~^, ~}~%" cbtics)
467 (if cbtics
468 (format dest "set cbtics ~f~%" cbtics)
469 (format dest "unset cbtics~%")))))
470 ;; axes ranges and style
471 (when (and (getf plot-options :x) (listp (getf plot-options :x)))
472 (format dest "set xrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options :x)))
473 (when (and (getf plot-options :y) (listp (getf plot-options :y)))
474 (format dest "set yrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options :y)))
475 (when (and (getf plot-options :z) (listp (getf plot-options :z)))
476 (format dest "set zrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options :z)))
477 (when (and (string= (getf plot-options :type) "plot2d")
478 (member :axes plot-options))
479 (if (getf plot-options :axes)
480 (case (getf plot-options :axes)
481 ($x (format dest "set xzeroaxis~%"))
482 ($y (format dest "set yzeroaxis~%"))
483 ($solid (format dest "set zeroaxis lt -1~%"))
484 (t (format dest "set zeroaxis~%")))))
485 ;; title and labels
486 (when (getf plot-options :title)
487 (format dest "set title \"~a\"~%" (getf plot-options :title)))
488 (when (getf plot-options :label)
489 (dolist (label (getf plot-options :label))
490 (when (and (listp label) (= (length label) 4))
491 (format dest "set label ~s at ~{~f~^, ~}~%"
492 (cadr label) (cddr label)))))
493 ;; identifier for missing data
494 (format dest "set datafile missing ~s~%" *missing-data-indicator*))))
495 ;;returns a list with the name of the file created, or nil
496 (if (null (second terminal-file))
497 nil (list (second terminal-file)))))
499 (defmethod plot2d-command ((plot gnuplot-plot) fun options range)
500 ;; Compute points to plot for each element of FUN.
501 ;; If no plottable points are found, end with an error.
502 (let (points-lists)
503 (setq points-lists
504 (mapcar #'(lambda (f) (cdr (draw2d f range options))) (cdr fun)))
505 (when (= (count-if #'(lambda (x) x) points-lists) 0)
506 (merror (intl:gettext "plot2d: nothing to plot.~%")))
507 (let ((legends-new) (legends (getf options :legend)))
508 (unless (null legends)
509 (dotimes (i (length legends))
510 (unless (null (cdr (nth i points-lists)))
511 (push (nth i legends) legends-new)))
512 (setf (getf options :legend) (reverse legends-new))))
513 (setf
514 (slot-value plot 'data)
515 (concatenate
516 'string
517 (slot-value plot 'data)
518 (with-output-to-string (st)
519 (unless (or (getf options :logy)
520 (and (getf options :y) (listp (getf options :y))))
521 (let (y ymin ymax)
522 (dolist (points-list points-lists)
523 (dotimes (i (/ (length points-list) 2))
524 (setq y (nth (1+ (* i 2)) points-list))
525 (when (numberp y)
526 (if (numberp ymin)
527 (if (numberp ymax)
528 (progn
529 (when (< y ymin) (setq ymin y))
530 (when (> y ymax) (setq ymax y)))
531 (if (< y ymin)
532 (setq ymax ymin ymin y)
533 (setq ymax y)))
534 (if (numberp ymax)
535 (if (> y ymax)
536 (setq ymin ymax ymax y)
537 (setq ymin y))
538 (setq ymin y))))))
539 (when (and (numberp ymin) (numberp ymax) (< ymin ymax))
540 (psetq ymin (- (* 1.05 ymin) (* 0.05 ymax))
541 ymax (- (* 1.05 ymax) (* 0.05 ymin)))
542 (format st "set yrange [~,,,,,,'eg: ~,,,,,,'eg]~%" ymin ymax))))
543 ;; user's commands; may overule any of the previous settings
544 (when (and (getf options :gnuplot_postamble)
545 (> (length (getf options :gnuplot_postamble)) 0))
546 (format st "~a~%" (getf options :gnuplot_postamble)))
547 ;; plot command
548 (format st "plot")
549 (when (getf options :x)
550 (format st " [~{~,,,,,,'eg~^ : ~}]" (getf options :x)))
551 (when (getf options :y)
552 (unless (getf options :x)
553 (format st " []"))
554 (format st " [~{~,,,,,,'eg~^ : ~}]" (getf options :y)))
555 (let ((legend (getf options :legend))
556 (colors (getf options :color))
557 (types (getf options :point_type))
558 (styles (getf options :style))
559 (i 0) style plot-name)
560 (unless (listp legend) (setq legend (list legend)))
561 (unless (listp colors) (setq colors (list colors)))
562 (unless (listp styles) (setq styles (list styles)))
563 (loop for v in (cdr fun) for points-list in points-lists do
564 (when points-list
565 (when ($listp (car points-list))
566 (dolist (level (cdar points-list))
567 (if styles
568 (setq style (nth (mod i (length styles)) styles))
569 (setq style nil))
570 (when ($listp style) (setq style (cdr style)))
571 (incf i)
572 (setq plot-name (ensure-string level))
573 (when (> i 1) (format st ","))
574 (format st " '-'")
575 (format st " title ~s " plot-name)
576 (format st (gnuplot-curve-style style colors types i)))
577 (return))
578 (if styles
579 (setq style (nth (mod i (length styles)) styles))
580 (setq style nil))
581 (when ($listp style) (setq style (cdr style)))
582 (incf i)
583 ;; label the expression according to the legend,
584 ;; unless it is "false" or there is only one expression
585 (if (member :legend options)
586 (setq plot-name
587 (if (first legend)
588 (ensure-string
589 (nth (mod (- i 1) (length legend)) legend)) nil))
590 (if (= 2 (length fun))
591 (setq plot-name nil)
592 (progn
593 (setq
594 plot-name
595 (with-output-to-string (pn)
596 (cond ((atom v) (format pn "~a" ($sconcat v)))
597 ((eq (second v) '$parametric)
598 (format pn "~a, ~a"
599 ($sconcat (third v))
600 ($sconcat (fourth v))))
601 ((eq (second v) '$discrete)
602 (format pn "discrete~a" i))
603 (t (format pn "~a" ($sconcat v))))))
604 (when (> (length plot-name) 50)
605 (setq plot-name (format nil "fun~a" i))))))
606 (when (> i 1) (format st ","))
607 (format st " '-'")
608 (if plot-name
609 (format st " title ~s " plot-name)
610 (format st " notitle "))
611 (format st (gnuplot-curve-style style colors types i)))))
612 ;; Parses points data
613 (format st "~%")
614 (let (in-discontinuity points)
615 (loop for points-list in points-lists do
616 (when points-list
617 ;; case "contour" with several plots in one list
618 (when ($listp (car points-list))
619 (dolist (level (cdr points-list))
620 (loop for (v w) on (cdr level) by #'cddr do
621 (cond ((eq v 'moveto)
622 ;; A blank line means a discontinuity
623 (if (null in-discontinuity)
624 (progn
625 (format st "~%")
626 (setq in-discontinuity t))))
628 (format st "~,,,,,,'eg ~,,,,,,'eg ~%" v w)
629 (setq points t)
630 (setq in-discontinuity nil))))
631 (if (and (null points)
632 (first (getf options :x))
633 (first (getf options :y)))
634 (format st "~,,,,,,'eg ~,,,,,,'eg ~%"
635 (first (getf options :x))
636 (first (getf options :y))))
637 (format st "e~%"))
638 (return))
639 ;; other cases with only one plot per list
640 (loop for (v w) on points-list by #'cddr do
641 (cond ((eq v 'moveto)
642 ;; A blank line means a discontinuity
643 (if (null in-discontinuity)
644 (progn
645 (format st "~%")
646 (setq in-discontinuity t))))
648 (format st "~,,,,,,'eg ~,,,,,,'eg ~%" v w)
649 (setq points t)
650 (setq in-discontinuity nil))))
651 (if (and (null points)
652 (first (getf options :x)) (first (getf options :y)))
653 (format st "~,,,,,,'eg ~,,,,,,'eg ~%"
654 (first (getf options :x))
655 (first (getf options :y)))))
656 (when points-list (format st "e~%")))))))))
658 (defmethod plot3d-command ((plot gnuplot-plot) functions options titles)
659 (let ((i 0) fun xrange yrange lvars trans (n (length functions)))
660 (setf
661 (slot-value plot 'data)
662 (concatenate
663 'string
664 (slot-value plot 'data)
665 (with-output-to-string ($pstream)
666 (format $pstream "~a"
667 (gnuplot-plot3d-command "-" (getf options :palette)
668 (getf options :gnuplot_curve_styles)
669 (getf options :color)
670 titles n))
671 ;; generate the mesh points for each surface in the functions stack
672 (dolist (f functions)
673 (setq i (+ 1 i))
674 (setq fun (first f))
675 (setq xrange (second f))
676 (setq yrange (third f))
677 (if ($listp fun)
678 (progn
679 (setq trans
680 ($make_transform `((mlist) ,(second xrange)
681 ,(second yrange) $z)
682 (second fun) (third fun) (fourth fun)))
683 (setq fun '$zero_fun))
684 (let*
685 ((x0 (third xrange))
686 (x1 (fourth xrange))
687 (y0 (third yrange))
688 (y1 (fourth yrange))
689 (xmid (+ x0 (/ (- x1 x0) 2)))
690 (ymid (+ y0 (/ (- y1 y0) 2))))
691 (setq lvars `((mlist) ,(second xrange) ,(second yrange)))
692 (setq fun (coerce-float-fun fun lvars "plot3d"))
693 ;; Evaluate FUN at the middle point of the range.
694 ;; Looking at a single point is somewhat unreliable.
695 ;; Call FUN with numerical arguments (symbolic arguments may
696 ;; fail due to trouble computing real/imaginary parts for
697 ;; complicated expressions, or it may be a numerical function)
698 (when (cdr ($listofvars (mfuncall fun xmid ymid)))
699 (mtell
700 (intl:gettext
701 "plot3d: expected <expr. of v1 and v2>, [v1,min,max], [v2,min,max]~%"))
702 (mtell
703 (intl:gettext
704 "plot3d: keep going and hope for the best.~%")))))
705 (let* ((pl
706 (draw3d
707 fun (third xrange) (fourth xrange) (third yrange)
708 (fourth yrange) (first (getf options :grid))
709 (second (getf options :grid))))
710 (ar (polygon-pts pl)))
711 (declare (type (cl:array t) ar))
712 (when trans (mfuncall trans ar))
713 (when (getf options :transform_xy)
714 (mfuncall (getf options :transform_xy) ar))
715 (output-points pl (first (getf options :grid)))
716 (format $pstream "e~%"))))))))
718 (defmethod plot-shipout ((plot gnuplot-plot) options &optional output-file)
719 (case (getf options :plot_format)
720 ($gnuplot
721 (let ((file (plot-set-gnuplot-script-file-name options)))
722 (with-open-file (fl
723 #+sbcl (sb-ext:native-namestring file)
724 #-sbcl file
725 :direction :output :if-exists :supersede)
726 (format fl "~a" (slot-value plot 'data)))
727 (gnuplot-process options file output-file)
728 (cons '(mlist) (cons file output-file))))
729 ($gnuplot_pipes
730 (send-gnuplot-command (slot-value plot 'data))
731 (when output-file
732 (send-gnuplot-command "unset output")
733 (cons '(mlist) output-file)))))