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"
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.
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.
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,
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
)
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
)))
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)
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
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
89 (format st
"with dots")
91 (format st
" lt ~d" (gnuplot-color colors
(second style
)))
92 (format st
" lt ~d" (gnuplot-color colors i
))))
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
))))
99 (format st
"with lines")
100 (if (realp (second style
))
101 (format st
" lw ~f" (second style
)))
103 (format st
" lt ~d" (gnuplot-color colors
(third style
)))
104 (format st
" lt ~d" (gnuplot-color colors i
))))
106 (format st
"with points")
107 (if (realp (second style
))
108 (format st
" ps ~f" (/ (second style
) 2))
109 (format st
" ps 1.5"))
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
))))
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"))
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
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
)
153 (case (length (rest palette
))
154 (2 (setq gray
(second palette
)) (setq range
(third palette
)))
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
)))
168 "palette: ~M must be followed by four numbers.")
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
)
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
))
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
))
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
))
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
))
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
205 ;; Maxima list of numbers and colors (((mlist) ni ci) ...)
206 ((listp (first colors
))
207 (setq colors
(sort colors
#'< :key
#'cadr
))
209 (setq map
(cons (rgb-color (third (nth i colors
))) ;; color
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
216 ;; list of only colors
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
)))
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
"~%"))
238 (setq style
(ensure-string (nth (mod i
(length gstyles
)) gstyles
)))
240 (format nil
"with lines lt ~a" (gnuplot-color colors i
)))))
241 (when (> i
1) (format out
", "))
243 (setq title
(nth (mod i
(length titles
)) titles
))
245 (format out
"~s title ~s ~a" file title style
)))))
247 (defun gnuplot-terminal-and-file (plot-options)
249 (if (getf plot-options
:gnuplot_strings
) "enhanced" "noenhanced"))
250 terminal-command out-file
(preserve-file t
))
252 ((getf plot-options
:svg_file
)
253 (if (getf plot-options
:gnuplot_svg_term_command
)
254 (setq terminal-command
255 (getf plot-options
:gnuplot_svg_term_command
))
256 (setq terminal-command
257 (format nil
"set term svg font \",14\" ~a" gstrings
)))
258 (setq out-file
(getf plot-options
:svg_file
)))
259 ((getf plot-options
:png_file
)
260 (if (getf plot-options
:gnuplot_png_term_command
)
261 (setq terminal-command
262 (getf plot-options
:gnuplot_png_term_command
))
263 (setq terminal-command
264 (format nil
"set term pngcairo font \",12\" ~a" gstrings
)))
265 (setq out-file
(getf plot-options
:png_file
)))
266 ((getf plot-options
:pdf_file
)
267 (if (getf plot-options
:gnuplot_pdf_term_command
)
268 (setq terminal-command
269 (getf plot-options
:gnuplot_pdf_term_command
))
270 (setq terminal-command
271 (format nil
"set term pdfcairo color solid lw 3 size 17.2 cm, 12.9 cm font \",18\" ~a" gstrings
)))
272 (setq out-file
(getf plot-options
:pdf_file
)))
273 ((getf plot-options
:ps_file
)
274 (if (getf plot-options
:gnuplot_ps_term_command
)
275 (setq terminal-command
276 (getf plot-options
:gnuplot_ps_term_command
))
277 (setq terminal-command
278 (format nil
"set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings
)))
279 (setq out-file
(getf plot-options
:ps_file
)))
280 ((eq (getf plot-options
:gnuplot_term
) '$ps
)
281 (if (getf plot-options
:gnuplot_ps_term_command
)
282 (setq terminal-command
283 (getf plot-options
:gnuplot_ps_term_command
))
284 (setq terminal-command
285 (format nil
"set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings
)))
286 (if (getf plot-options
:gnuplot_out_file
)
287 (setq out-file
(getf plot-options
:gnuplot_out_file
))
288 (setq out-file
"maxplot.ps")))
289 ((eq (getf plot-options
:gnuplot_term
) '$dumb
)
290 (if (getf plot-options
:gnuplot_dumb_term_command
)
291 (setq terminal-command
292 (getf plot-options
:gnuplot_ps_term_command
))
293 (setq terminal-command
"set term dumb 79 22"))
294 (if (getf plot-options
:gnuplot_out_file
)
295 (setq out-file
(getf plot-options
:gnuplot_out_file
))
296 (setq out-file
"maxplot.txt")))
297 ((eq (getf plot-options
:gnuplot_term
) '$default
)
298 (if (getf plot-options
:gnuplot_default_term_command
)
299 (setq terminal-command
300 (getf plot-options
:gnuplot_default_term_command
))
301 (setq terminal-command
302 (if (getf plot-options
:window
)
303 (format nil
"set term GNUTERM ~d ~a~%"
304 (getf plot-options
:window
) gstrings
)
305 (format nil
"set term GNUTERM ~a~%" gstrings
)))))
306 ((getf plot-options
:gnuplot_term
)
309 (format nil
"set term ~(~a~)"
310 (ensure-string (getf plot-options
:gnuplot_term
))))
311 (if (getf plot-options
:gnuplot_out_file
)
312 (setq out-file
(getf plot-options
:gnuplot_out_file
))
313 (setq preserve-file nil
315 (format nil
"maxplot.~(~a~)"
316 (get-gnuplot-term (getf plot-options
:gnuplot_term
)))))))
318 (unless (null out-file
) (setq out-file
(plot-file-path out-file preserve-file plot-options
)))
319 (list terminal-command out-file
)))
321 (defmethod plot-preamble ((plot gnuplot-plot
) plot-options
)
322 (let ((palette (getf plot-options
:palette
))
323 (meshcolor (if (member :mesh_lines_color plot-options
)
324 (getf plot-options
:mesh_lines_color
)
325 '$black
)) terminal-file
)
326 (when (find 'mlist palette
:key
#'car
) (setq palette
(list palette
)))
327 ;; sets-up terminal command and output file name
328 (setq terminal-file
(gnuplot-terminal-and-file plot-options
))
330 (slot-value plot
'data
)
333 (slot-value plot
'data
)
334 (with-output-to-string (dest)
335 ;; reset initial state
336 (format dest
"reset~%unset output~%unset multiplot~%")
338 (when (and (getf plot-options
:gnuplot_preamble
)
339 (> (length (getf plot-options
:gnuplot_preamble
)) 0))
340 (format dest
"~a~%" (getf plot-options
:gnuplot_preamble
)))
341 ;; Don't round numbers with absolute value less than 1e-8 to zero
342 (format dest
"set zero 0.0~%")
343 ;; prints terminal and output commands
344 (when (first terminal-file
)
345 (format dest
"~a~%" (first terminal-file
)))
346 (when (second terminal-file
)
347 (format dest
"set output ~s~%" (second terminal-file
)))
348 ;; options specific to plot3d
349 (when (string= (getf plot-options
:type
) "plot3d")
350 (format dest
"set xyplane relative 0~%")
356 "if (GPVAL_VERSION < 5.0) set style line 100 lt rgb ~s lw 1; set pm3d hidden3d 100~%"
357 (rgb-color meshcolor
))
359 "if (GPVAL_VERSION >= 5.0) set pm3d hidden3d 100 border lw 0.5 lt rgb ~s~%"
360 (rgb-color meshcolor
))
361 (unless (getf plot-options
:gnuplot_4_0
)
362 (format dest
"set pm3d depthorder~%")))
363 (format dest
"set pm3d~%"))
364 (format dest
"unset hidden3d~%")
365 (format dest
"set palette ~a~%"
366 (gnuplot-palette (rest (first palette
)))))
367 (format dest
"set hidden3d~%"))
368 (let ((elev (getf plot-options
:elevation
))
369 (azim (getf plot-options
:azimuth
)))
372 (format dest
"set view ~d" elev
)
373 (format dest
"set view "))
374 (when azim
(format dest
", ~d" azim
))
375 (format dest
"~%"))))
376 ;; color_bar can be used by plot3d or plot2d
377 (unless (getf plot-options
:color_bar
)
378 (format dest
"unset colorbox~%"))
379 ;; ----- BEGIN GNUPLOT 4.0 WORK-AROUND -----
380 ;; When the expression plotted is constant, Gnuplot 4.0 fails
381 ;; with a division by 0. Explicitly assigning cbrange prevents
382 ;; the error. Also set zrange to match cbrange.
383 (when (floatp (getf plot-options
:const_expr
))
385 dest
"set cbrange [~a : ~a]~%"
386 (1- (getf plot-options
:const_expr
))
387 (1+ (getf plot-options
:const_expr
)))
389 dest
"set zrange [~a : ~a]~%"
390 (1- (getf plot-options
:const_expr
))
391 (1+ (getf plot-options
:const_expr
))))
392 ;; ----- END GNUPLOT 4.0 WORK-AROUND -----
394 (when (getf plot-options
:logx
) (format dest
"set log x~%"))
395 (when (getf plot-options
:logy
) (format dest
"set log y~%"))
396 ;; axes labels and legend
397 (when (getf plot-options
:xlabel
)
398 (format dest
"set xlabel ~s~%" (getf plot-options
:xlabel
)))
399 (when (getf plot-options
:ylabel
)
400 (format dest
"set ylabel ~s~%" (getf plot-options
:ylabel
)))
401 (when (getf plot-options
:zlabel
)
402 (format dest
"set zlabel ~s~%" (getf plot-options
:zlabel
)))
403 (when (and (member :legend plot-options
)
404 (null (getf plot-options
:legend
)))
405 (format dest
"unset key~%"))
407 (when (and (member :box plot-options
) (not (getf plot-options
:box
)))
408 (format dest
"unset border~%")
409 (if (and (getf plot-options
:axes
)
410 (string= (getf plot-options
:type
) "plot2d"))
411 (format dest
"set xtics axis~%set ytics axis~%set ztics axis~%")
412 (format dest
"unset xtics~%unset ytics~%unset ztics~%")))
413 ;; 2d grid (specific to plot2d)
414 (when (string= (getf plot-options
:type
) "plot2d")
415 (format dest
"set grid front~%")
416 (if (getf plot-options
:grid2d
)
417 (format dest
"set grid~%")
418 (format dest
"unset grid~%"))
419 ;; plot size and aspect ratio for plot2d
420 (if (getf plot-options
:same_xy
)
421 (format dest
"set size ratio -1~%")
422 (if (getf plot-options
:yx_ratio
)
423 (format dest
"set size ratio ~f~%"
424 (getf plot-options
:yx_ratio
))
425 (if (not (getf plot-options
:xy_scale
))
426 ;; emit the default only if there is no xy_scale specified.
427 (format dest
"set size ratio 0.75~%"))))
428 (if (and (getf plot-options
:xy_scale
)
429 (listp (getf plot-options
:xy_scale
)))
430 (format dest
"set size ~{~f~^, ~}~%"
431 (getf plot-options
:xy_scale
))))
432 ;; plot size and aspect ratio for plot3d
433 (when (string= (getf plot-options
:type
) "plot3d")
434 (when (getf plot-options
:same_xy
)
435 (format dest
"set view equal xy~%"))
436 (when (getf plot-options
:same_xyz
)
437 (format dest
"set view equal xyz~%"))
438 (when (getf plot-options
:zmin
)
439 (format dest
"set xyplane at ~f~%" (getf plot-options
:zmin
))))
441 (when (member :xtics plot-options
)
442 (let ((xtics (getf plot-options
:xtics
)))
444 (format dest
"set xtics ~{~f~^, ~}~%" xtics
)
446 (format dest
"set xtics ~f~%" xtics
)
447 (format dest
"unset xtics~%")))))
448 (when (member :ytics plot-options
)
449 (let ((ytics (getf plot-options
:ytics
)))
451 (format dest
"set ytics ~{~f~^, ~}~%" ytics
)
453 (format dest
"set ytics ~f~%" ytics
)
454 (format dest
"unset ytics~%")))))
455 (when (member :ztics plot-options
)
456 (let ((ztics (getf plot-options
:ztics
)))
458 (format dest
"set ztics ~{~f~^, ~}~%" ztics
)
460 (format dest
"set ztics ~f~%" ztics
)
461 (format dest
"unset ztics~%")))))
462 (when (member :color_bar_tics plot-options
)
463 (let ((cbtics (getf plot-options
:color_bar_tics
)))
465 (format dest
"set cbtics ~{~f~^, ~}~%" cbtics
)
467 (format dest
"set cbtics ~f~%" cbtics
)
468 (format dest
"unset cbtics~%")))))
469 ;; axes ranges and style
470 (when (and (getf plot-options
:x
) (listp (getf plot-options
:x
)))
471 (format dest
"set xrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
:x
)))
472 (when (and (getf plot-options
:y
) (listp (getf plot-options
:y
)))
473 (format dest
"set yrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
:y
)))
474 (when (and (getf plot-options
:z
) (listp (getf plot-options
:z
)))
475 (format dest
"set zrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
:z
)))
476 (when (and (string= (getf plot-options
:type
) "plot2d")
477 (member :axes plot-options
))
478 (if (getf plot-options
:axes
)
479 (case (getf plot-options
:axes
)
480 ($x
(format dest
"set xzeroaxis~%"))
481 ($y
(format dest
"set yzeroaxis~%"))
482 ($solid
(format dest
"set zeroaxis lt -1~%"))
483 (t (format dest
"set zeroaxis~%")))))
485 (when (getf plot-options
:title
)
486 (format dest
"set title ~s~%" (getf plot-options
:title
)))
487 (when (getf plot-options
:label
)
488 (dolist (label (getf plot-options
:label
))
489 (when (and (listp label
) (= (length label
) 4))
490 (format dest
"set label ~s at ~{~f~^, ~}~%"
491 (cadr label
) (cddr label
)))))
492 ;; identifier for missing data
493 (format dest
"set datafile missing ~s~%" *missing-data-indicator
*))))
494 ;;returns a list with the name of the file created, or nil
495 (if (null (second terminal-file
))
496 nil
(list (second terminal-file
)))))
498 (defmethod plot2d-command ((plot gnuplot-plot
) fun options range
)
499 ;; Compute points to plot for each element of FUN.
500 ;; If no plottable points are found, end with an error.
503 (mapcar #'(lambda (f) (cdr (draw2d f range options
))) (cdr fun
)))
504 (when (= (count-if #'(lambda (x) x
) points-lists
) 0)
505 (merror (intl:gettext
"plot2d: nothing to plot.~%")))
507 (slot-value plot
'data
)
510 (slot-value plot
'data
)
511 (with-output-to-string (st)
512 (unless (or (getf options
:logy
)
513 (and (getf options
:y
) (listp (getf options
:y
))))
515 (dolist (points-list points-lists
)
516 (dotimes (i (/ (length points-list
) 2))
517 (setq y
(nth (1+ (* i
2)) points-list
))
522 (when (< y ymin
) (setq ymin y
))
523 (when (> y ymax
) (setq ymax y
)))
525 (setq ymax ymin ymin y
)
529 (setq ymin ymax ymax y
)
532 (when (and (numberp ymin
) (numberp ymax
) (< ymin ymax
))
533 (psetq ymin
(- (* 1.05 ymin
) (* 0.05 ymax
))
534 ymax
(- (* 1.05 ymax
) (* 0.05 ymin
)))
535 (format st
"set yrange [~,,,,,,'eg: ~,,,,,,'eg]~%" ymin ymax
))))
536 ;; user's commands; may overule any of the previous settings
537 (when (and (getf options
:gnuplot_postamble
)
538 (> (length (getf options
:gnuplot_postamble
)) 0))
539 (format st
"~a~%" (getf options
:gnuplot_postamble
)))
542 (when (getf options
:x
)
543 (format st
" [~{~,,,,,,'eg~^ : ~}]" (getf options
:x
)))
544 (when (getf options
:y
)
545 (unless (getf options
:x
)
547 (format st
" [~{~,,,,,,'eg~^ : ~}]" (getf options
:y
)))
548 (let ((legend (getf options
:legend
))
549 (colors (getf options
:color
))
550 (types (getf options
:point_type
))
551 (styles (getf options
:style
))
552 (i 0) style plot-name
)
553 (unless (listp legend
) (setq legend
(list legend
)))
554 (unless (listp colors
) (setq colors
(list colors
)))
555 (unless (listp styles
) (setq styles
(list styles
)))
556 (loop for v in
(cdr fun
) for points-list in points-lists do
558 (when ($listp
(car points-list
))
559 (dolist (level (cdar points-list
))
561 (setq style
(nth (mod i
(length styles
)) styles
))
563 (when ($listp style
) (setq style
(cdr style
)))
565 (setq plot-name
(ensure-string level
))
566 (when (> i
1) (format st
","))
568 (format st
" title ~s " plot-name
)
569 (format st
(gnuplot-curve-style style colors types i
)))
572 (setq style
(nth (mod i
(length styles
)) styles
))
574 (when ($listp style
) (setq style
(cdr style
)))
576 ;; label the expression according to the legend,
577 ;; unless it is "false" or there is only one expression
578 (if (member :legend options
)
582 (nth (mod (- i
1) (length legend
)) legend
)) nil
))
583 (if (= 2 (length fun
))
588 (with-output-to-string (pn)
589 (cond ((atom v
) (format pn
"~a" ($sconcat v
)))
590 ((eq (second v
) '$parametric
)
593 ($sconcat
(fourth v
))))
594 ((eq (second v
) '$discrete
)
595 (format pn
"discrete~a" i
))
596 (t (format pn
"~a" ($sconcat v
))))))
597 (when (> (length plot-name
) 50)
598 (setq plot-name
(format nil
"fun~a" i
))))))
599 (when (> i
1) (format st
","))
602 (format st
" title ~s " plot-name
)
603 (format st
" notitle "))
604 (format st
(gnuplot-curve-style style colors types i
)))))
605 ;; Parses points data
607 (let (in-discontinuity points
)
608 (loop for points-list in points-lists do
610 ;; case "contour" with several plots in one list
611 (when ($listp
(car points-list
))
612 (dolist (level (cdr points-list
))
613 (loop for
(v w
) on
(cdr level
) by
#'cddr do
614 (cond ((eq v
'moveto
)
615 ;; A blank line means a discontinuity
616 (if (null in-discontinuity
)
619 (setq in-discontinuity t
))))
621 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%" v w
)
623 (setq in-discontinuity nil
))))
624 (if (and (null points
)
625 (first (getf options
:x
))
626 (first (getf options
:y
)))
627 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%"
628 (first (getf options
:x
))
629 (first (getf options
:y
))))
632 ;; other cases with only one plot per list
633 (loop for
(v w
) on points-list by
#'cddr do
634 (cond ((eq v
'moveto
)
635 ;; A blank line means a discontinuity
636 (if (null in-discontinuity
)
639 (setq in-discontinuity t
))))
641 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%" v w
)
643 (setq in-discontinuity nil
))))
644 (if (and (null points
)
645 (first (getf options
:x
)) (first (getf options
:y
)))
646 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%"
647 (first (getf options
:x
))
648 (first (getf options
:y
)))))
649 (when points-list
(format st
"e~%")))))))))
651 (defmethod plot3d-command ((plot gnuplot-plot
) functions options titles
)
652 (let ((i 0) fun xrange yrange lvars trans
(n (length functions
)))
654 (slot-value plot
'data
)
657 (slot-value plot
'data
)
658 (with-output-to-string ($pstream
)
659 (format $pstream
"~a"
660 (gnuplot-plot3d-command "-" (getf options
:palette
)
661 (getf options
:gnuplot_curve_styles
)
662 (getf options
:color
)
664 ;; generate the mesh points for each surface in the functions stack
665 (dolist (f functions
)
668 (setq xrange
(second f
))
669 (setq yrange
(third f
))
673 ($make_transform
`((mlist) ,(second xrange
)
675 (second fun
) (third fun
) (fourth fun
)))
676 (setq fun
'$zero_fun
))
682 (xmid (+ x0
(/ (- x1 x0
) 2)))
683 (ymid (+ y0
(/ (- y1 y0
) 2))))
684 (setq lvars
`((mlist) ,(second xrange
) ,(second yrange
)))
685 (setq fun
(coerce-float-fun fun lvars
"plot3d"))
686 ;; Evaluate FUN at the middle point of the range.
687 ;; Looking at a single point is somewhat unreliable.
688 ;; Call FUN with numerical arguments (symbolic arguments may
689 ;; fail due to trouble computing real/imaginary parts for
690 ;; complicated expressions, or it may be a numerical function)
691 (when (cdr ($listofvars
(mfuncall fun xmid ymid
)))
694 "plot3d: expected <expr. of v1 and v2>, [v1,min,max], [v2,min,max]~%"))
697 "plot3d: keep going and hope for the best.~%")))))
700 fun
(third xrange
) (fourth xrange
) (third yrange
)
701 (fourth yrange
) (first (getf options
:grid
))
702 (second (getf options
:grid
))))
703 (ar (polygon-pts pl
)))
704 (declare (type (cl:array t
) ar
))
705 (when trans
(mfuncall trans ar
))
706 (when (getf options
:transform_xy
)
707 (mfuncall (getf options
:transform_xy
) ar
))
708 (output-points pl
(first (getf options
:grid
)))
709 (format $pstream
"e~%"))))))))
711 (defmethod plot-shipout ((plot gnuplot-plot
) options
&optional output-file
)
712 (case (getf options
:plot_format
)
714 (let ((file (plot-set-gnuplot-script-file-name options
)))
716 #+sbcl
(sb-ext:native-namestring file
)
718 :direction
:output
:if-exists
:supersede
)
719 (format fl
"~a" (slot-value plot
'data
)))
720 (gnuplot-process options file output-file
)
721 (cons '(mlist) (cons file output-file
))))
723 (send-gnuplot-command (slot-value plot
'data
))
725 (send-gnuplot-command "unset output")
726 (cons '(mlist) output-file
)))))