1 ;; gnuplot_def.lisp: routines for Maxima's interface to gnuplot
2 ;; Copyright (C) 2007-2021 J. Villate
3 ;; Time-stamp: "2024-03-25 09:10:05 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, pointinterval
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 (if (integerp (sixth style
))
130 (format st
" pointinterval ~d" (sixth style
)))
132 (t (format st
"with lines lt ~d" (gnuplot-color colors i
))))))
135 (defun gnuplot-palette (palette)
136 ;; palette should be a list starting with one of the symbols: hue,
137 ;; saturation, value, gray or gradient.
139 ;; If the symbol is gray, it should be followed by two floating point
140 ;; numbers that indicate the initial gray level and the interval of
143 ;; If the symbol is one of hue, saturation or value, it must be followed
144 ;; by three numbers that specify the hue, saturation and value for the
145 ;; initial color, and a fourth number that gives the range of values for
146 ;; the increment of hue, saturation or value.
147 ;; The values for the initial hue, saturation, value and grayness should
148 ;; be within 0 and 1, while the range can be higher or even negative.
150 ;; If the symbol is gradient, it must be followed by either a list of valid
151 ;; colors or by a list of lists with two elements, a number and a valid color.
153 (unless (listp palette
) (setq palette
(list palette
)))
154 (let (hue sat val gray range fun
)
155 (case (first palette
)
157 (case (length (rest palette
))
158 (2 (setq gray
(second palette
)) (setq range
(third palette
)))
161 "palette: gray must be followed by two numbers."))))
162 (when (or (< gray
0) (> gray
1))
163 (setq gray
(- gray
(floor gray
)))))
164 (($hue $saturation $value
)
165 (case (length (rest palette
))
166 (4 (setq hue
(second palette
))
167 (setq sat
(third palette
))
168 (setq val
(fourth palette
))
169 (setq range
(fifth palette
)))
172 "palette: ~M must be followed by four numbers.")
174 (when (or (< hue
0) (> hue
1)) (setq hue
(- hue
(floor hue
))))
175 (when (or (< sat
0) (> sat
1)) (setq sat
(- sat
(floor sat
))))
176 (when (or (< val
0) (> val
1)) (setq val
(- val
(floor val
))))))
177 (with-output-to-string (st)
178 (case (first palette
)
180 (if (or (< (+ hue range
) 0) (> (+ hue range
) 1))
181 (setq fun
(format nil
"~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
182 hue range hue range
))
183 (setq fun
(format nil
"~,3f+~,3f*gray" hue range
)))
184 (format st
"model HSV functions ~a, ~,3f, ~,3f" fun sat val
))
186 (if (or (< (+ sat range
) 0) (> (+ sat range
) 1))
187 (setq fun
(format nil
"~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
188 sat range sat range
))
189 (setq fun
(format nil
"~,3f+~,3f*gray" sat range
)))
190 (format st
"model HSV functions ~,3f, ~a, ~,3f" hue fun val
))
192 (if (or (< (+ val range
) 0) (> (+ val range
) 1))
193 (setq fun
(format nil
"~,3f+~,3f*gray" val range
))
194 (setq fun
(format nil
"~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
195 val range val range
)))
196 (format st
"model HSV functions ~,3f, ~,3f, ~a" hue sat fun
))
198 (if (or (< (+ gray range
) 0) (> (+ gray range
) 1))
199 (setq fun
(format nil
"~,3f+~,3f*gray" gray range
))
200 (setq fun
(format nil
"~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
201 gray range gray range
)))
202 (format st
"model RGB functions ~a, ~a, ~a" fun fun fun
))
205 (let* ((colors (rest palette
)) (n (length colors
)) (map nil
))
206 ;; map is constructed as (n1 c1 n2 c2 ... nj cj) where ni is a
207 ;; decreasing sequence of numbers (n1=1, nj=0) and ci are colors
209 ;; Maxima list of numbers and colors (((mlist) ni ci) ...)
210 ((listp (first colors
))
211 (setq colors
(sort colors
#'< :key
#'cadr
))
213 (setq map
(cons (rgb-color (third (nth i colors
))) ;; color
215 (/ (- (second (nth i colors
)) ;; ni minus
216 (second (first colors
))) ;; smallest ni
217 (- (second (nth (- n
1) colors
));; biggest
218 (second (first colors
)))) ;; - smallest
220 ;; list of only colors
222 (setq map
(cons (rgb-color (nth i colors
)) ;; color i
223 (cons (/ i
(1- n
)) map
)))))) ;; number i
225 ;; prints map with the format: nj, "cj", ...,n1, "c1"
226 (setq fun
(format nil
"~{~f ~s~^, ~}" (reverse map
)))
227 ;; outputs the string: defined (nj, "cj", ...,n1, "c1")
228 (format st
"defined (~a)" fun
)))
232 "palette: wrong keyword ~M. Must be hue, saturation, value, gray or gradient.")
233 (first palette
)))))))
235 (defun gnuplot-plot3d-command (file palette gstyles colors titles n
)
236 (let (title (style "with pm3d"))
237 (with-output-to-string (out)
238 (format out
"splot ")
239 (do ((i 1 (+ i
1))) ((> i n
) (format out
"~%"))
242 (setq style
(ensure-string (nth (mod i
(length gstyles
)) gstyles
)))
244 (format nil
"with lines lt ~a" (gnuplot-color colors i
)))))
245 (when (> i
1) (format out
", "))
247 (setq title
(nth (mod i
(length titles
)) titles
))
249 (format out
"~s title ~s ~a" file title style
)))))
251 (defun gnuplot-terminal-and-file (plot-options)
253 (if (getf plot-options
'$gnuplot_strings
) "enhanced" "noenhanced"))
254 (gnuplot-svg-background (getf plot-options
'$gnuplot_svg_background
))
255 terminal-command out-file
(preserve-file t
))
257 ((getf plot-options
'$svg_file
)
258 (if (getf plot-options
'$gnuplot_svg_term_command
)
259 (setq terminal-command
260 (getf plot-options
'$gnuplot_svg_term_command
))
261 (setq terminal-command
262 (format nil
"set term svg font \",14\" ~a~@[ background '~a'~]" gstrings gnuplot-svg-background
)))
263 (setq out-file
(getf plot-options
'$svg_file
)))
264 ((getf plot-options
'$png_file
)
265 (if (getf plot-options
'$gnuplot_png_term_command
)
266 (setq terminal-command
267 (getf plot-options
'$gnuplot_png_term_command
))
268 (setq terminal-command
269 (format nil
"set term pngcairo font \",12\" ~a" gstrings
)))
270 (setq out-file
(getf plot-options
'$png_file
)))
271 ((getf plot-options
'$pdf_file
)
272 (if (getf plot-options
'$gnuplot_pdf_term_command
)
273 (setq terminal-command
274 (getf plot-options
'$gnuplot_pdf_term_command
))
275 (setq terminal-command
276 (format nil
"set term pdfcairo color solid lw 3 size 17.2 cm, 12.9 cm font \",18\" ~a" gstrings
)))
277 (setq out-file
(getf plot-options
'$pdf_file
)))
278 ((getf plot-options
'$ps_file
)
279 (if (getf plot-options
'$gnuplot_ps_term_command
)
280 (setq terminal-command
281 (getf plot-options
'$gnuplot_ps_term_command
))
282 (setq terminal-command
283 (format nil
"set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings
)))
284 (setq out-file
(getf plot-options
'$ps_file
)))
285 ((eq (getf plot-options
'$gnuplot_term
) '$ps
)
286 (if (getf plot-options
'$gnuplot_ps_term_command
)
287 (setq terminal-command
288 (getf plot-options
'$gnuplot_ps_term_command
))
289 (setq terminal-command
290 (format nil
"set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\" ~a" gstrings
)))
291 (if (getf plot-options
'$gnuplot_out_file
)
292 (setq out-file
(getf plot-options
'$gnuplot_out_file
))
293 (setq out-file
(format nil
"~a.ps" (random-name 16)))))
294 ((eq (getf plot-options
'$gnuplot_term
) '$dumb
)
295 (if (getf plot-options
'$gnuplot_dumb_term_command
)
296 (setq terminal-command
297 (getf plot-options
'$gnuplot_ps_term_command
))
298 (setq terminal-command
"set term dumb 79 22"))
299 (if (getf plot-options
'$gnuplot_out_file
)
300 (setq out-file
(getf plot-options
'$gnuplot_out_file
))
301 (setq out-file
(format nil
"~a.txt" (random-name 16)))))
302 ((eq (getf plot-options
'$gnuplot_term
) '$default
)
303 (if (getf plot-options
'$gnuplot_default_term_command
)
304 (setq terminal-command
305 (getf plot-options
'$gnuplot_default_term_command
))
306 (setq terminal-command
307 (if (getf plot-options
'$window
)
308 (format nil
"set term GNUTERM ~d ~a~%"
309 (getf plot-options
'$window
) gstrings
)
310 (format nil
"set term GNUTERM ~a~%" gstrings
)))))
311 ((getf plot-options
'$gnuplot_term
)
314 (format nil
"set term ~(~a~)"
315 (ensure-string (getf plot-options
'$gnuplot_term
))))
316 (if (getf plot-options
'$gnuplot_out_file
)
317 (setq out-file
(getf plot-options
'$gnuplot_out_file
))
318 (setq preserve-file nil
320 (format nil
"maxplot.~(~a~)"
321 (get-gnuplot-term (getf plot-options
'$gnuplot_term
)))))))
323 (unless (null out-file
) (setq out-file
(plot-file-path out-file preserve-file plot-options
)))
324 (list terminal-command out-file
)))
326 (defmethod plot-preamble ((plot gnuplot-plot
) plot-options
)
327 (let ((palette (getf plot-options
'$palette
))
328 (meshcolor (if (member '$mesh_lines_color plot-options
)
329 (getf plot-options
'$mesh_lines_color
)
330 '$black
)) terminal-file
)
331 (when (find 'mlist palette
:key
#'car
) (setq palette
(list palette
)))
332 ;; sets-up terminal command and output file name
333 (setq terminal-file
(gnuplot-terminal-and-file plot-options
))
335 (slot-value plot
'data
)
338 (slot-value plot
'data
)
339 (with-output-to-string (dest)
340 ;; reset initial state
341 (format dest
"reset~%unset output~%unset multiplot~%set clip two~%")
343 (when (and (getf plot-options
'$gnuplot_preamble
)
344 (> (length (getf plot-options
'$gnuplot_preamble
)) 0))
345 (format dest
"~a~%" (getf plot-options
'$gnuplot_preamble
)))
346 ;; Don't round numbers with absolute value less than 1e-8 to zero
347 (format dest
"set zero 0.0~%")
348 ;; prints terminal and output commands
349 (when (first terminal-file
)
350 (format dest
"~a~%" (first terminal-file
)))
351 (when (second terminal-file
)
352 (format dest
"set output ~s~%" (second terminal-file
)))
353 ;; options specific to plot3d
354 (when (string= (getf plot-options
'$type
) "plot3d")
355 (format dest
"set xyplane relative 0~%")
361 "if (GPVAL_VERSION < 5.0) set style line 100 lt rgb ~s lw 1; set pm3d hidden3d 100~%"
362 (rgb-color meshcolor
))
364 "if ((GPVAL_VERSION >= 5.0) && (GPVAL_VERSION < 6.0)) set pm3d hidden3d 100 border lw 0.5 lt rgb ~s~%"
365 (rgb-color meshcolor
))
367 "if (GPVAL_VERSION >= 6.0) set pm3d hidden3d border lw 0.5 lt rgb ~s~%"
368 (rgb-color meshcolor
))
369 (unless (getf plot-options
'$gnuplot_4_0
)
370 (format dest
"set pm3d depthorder~%")))
371 (format dest
"set pm3d~%"))
372 (format dest
"unset hidden3d~%")
373 (format dest
"set palette ~a~%"
374 (gnuplot-palette (rest (first palette
)))))
375 (format dest
"set hidden3d~%"))
376 (let ((elev (getf plot-options
'$elevation
))
377 (azim (getf plot-options
'$azimuth
)))
380 (format dest
"set view ~d" elev
)
381 (format dest
"set view "))
382 (when azim
(format dest
", ~d" azim
))
383 (format dest
"~%"))))
384 ;; color_bar can be used by plot3d or plot2d
385 (unless (getf plot-options
'$color_bar
)
386 (format dest
"unset colorbox~%"))
387 ;; ----- BEGIN GNUPLOT 4.0 WORK-AROUND -----
388 ;; When the expression plotted is constant, Gnuplot 4.0 fails
389 ;; with a division by 0. Explicitly assigning cbrange prevents
390 ;; the error. Also set zrange to match cbrange.
391 (when (floatp (getf plot-options
'$const_expr
))
393 dest
"set cbrange [~a : ~a]~%"
394 (1- (getf plot-options
'$const_expr
))
395 (1+ (getf plot-options
'$const_expr
)))
397 dest
"set zrange [~a : ~a]~%"
398 (1- (getf plot-options
'$const_expr
))
399 (1+ (getf plot-options
'$const_expr
))))
400 ;; ----- END GNUPLOT 4.0 WORK-AROUND -----
402 (when (getf plot-options
'$logx
) (format dest
"set log x~%"))
403 (when (getf plot-options
'$logy
) (format dest
"set log y~%"))
404 ;; axes labels and legend
405 (when (getf plot-options
'$xlabel
)
406 (format dest
"set xlabel ~s~%" (getf plot-options
'$xlabel
)))
407 (when (getf plot-options
'$ylabel
)
408 (format dest
"set ylabel ~s~%" (getf plot-options
'$ylabel
)))
409 (when (getf plot-options
'$zlabel
)
410 (format dest
"set zlabel ~s~%" (getf plot-options
'$zlabel
)))
411 (when (and (member '$legend plot-options
)
412 (null (getf plot-options
'$legend
)))
413 (format dest
"unset key~%"))
415 (when (and (member '$box plot-options
) (not (getf plot-options
'$box
)))
416 (format dest
"unset border~%")
417 (if (and (getf plot-options
'$axes
)
418 (string= (getf plot-options
'$type
) "plot2d"))
419 (format dest
"set xtics axis~%set ytics axis~%set ztics axis~%")
420 (format dest
"unset xtics~%unset ytics~%unset ztics~%")))
421 ;; 2d grid (specific to plot2d)
422 (when (string= (getf plot-options
'$type
) "plot2d")
423 (format dest
"set grid front~%")
424 (if (getf plot-options
'$grid2d
)
425 (format dest
"set grid~%")
426 (format dest
"unset grid~%"))
427 ;; plot size and aspect ratio for plot2d
428 (if (getf plot-options
'$same_xy
)
429 (format dest
"set size ratio -1~%")
430 (if (getf plot-options
'$yx_ratio
)
431 (format dest
"set size ratio ~f~%"
432 (getf plot-options
'$yx_ratio
))
433 (if (not (getf plot-options
'$xy_scale
))
434 ;; emit the default only if there is no xy_scale specified.
435 (format dest
"set size ratio 0.75~%"))))
436 (if (and (getf plot-options
'$xy_scale
)
437 (listp (getf plot-options
'$xy_scale
)))
438 (format dest
"set size ~{~f~^, ~}~%"
439 (getf plot-options
'$xy_scale
))))
440 ;; plot size and aspect ratio for plot3d
441 (when (string= (getf plot-options
'$type
) "plot3d")
442 (when (getf plot-options
'$same_xy
)
443 (format dest
"set view equal xy~%"))
444 (when (getf plot-options
'$same_xyz
)
445 (format dest
"set view equal xyz~%"))
446 (when (getf plot-options
'$zmin
)
447 (format dest
"set xyplane at ~f~%" (getf plot-options
'$zmin
))))
449 (when (member '$xtics plot-options
)
450 (let ((xtics (getf plot-options
'$xtics
)))
452 (format dest
"set xtics ~{~f~^, ~}~%" xtics
)
454 (format dest
"set xtics ~f~%" xtics
)
455 (format dest
"unset xtics~%")))))
456 (when (member '$ytics plot-options
)
457 (let ((ytics (getf plot-options
'$ytics
)))
459 (format dest
"set ytics ~{~f~^, ~}~%" ytics
)
461 (format dest
"set ytics ~f~%" ytics
)
462 (format dest
"unset ytics~%")))))
463 (when (member '$ztics plot-options
)
464 (let ((ztics (getf plot-options
'$ztics
)))
466 (format dest
"set ztics ~{~f~^, ~}~%" ztics
)
468 (format dest
"set ztics ~f~%" ztics
)
469 (format dest
"unset ztics~%")))))
470 (when (member '$color_bar_tics plot-options
)
471 (let ((cbtics (getf plot-options
'$color_bar_tics
)))
473 (format dest
"set cbtics ~{~f~^, ~}~%" cbtics
)
475 (format dest
"set cbtics ~f~%" cbtics
)
476 (format dest
"unset cbtics~%")))))
477 ;; axes ranges and style
478 (when (and (getf plot-options
'$x
) (listp (getf plot-options
'$x
)))
479 (format dest
"set xrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
'$x
)))
480 (when (and (getf plot-options
'$y
) (listp (getf plot-options
'$y
)))
481 (format dest
"set yrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
'$y
)))
482 (when (and (getf plot-options
'$z
) (listp (getf plot-options
'$z
)))
483 (format dest
"set zrange [~{~,,,,,,'eg~^ : ~}]~%" (getf plot-options
'$z
)))
484 (when (and (string= (getf plot-options
'$type
) "plot2d")
485 (member '$axes plot-options
))
486 (if (getf plot-options
'$axes
)
487 (case (getf plot-options
'$axes
)
488 ($x
(format dest
"set xzeroaxis~%"))
489 ($y
(format dest
"set yzeroaxis~%"))
490 ($solid
(format dest
"set zeroaxis lt -1~%"))
491 (t (format dest
"set zeroaxis~%")))))
493 (when (getf plot-options
'$title
)
494 (format dest
"set title \"~a\"~%" (getf plot-options
'$title
)))
495 (when (getf plot-options
'$label
)
496 (dolist (label (getf plot-options
'$label
))
497 (when (and (listp label
) (= (length label
) 4))
498 (format dest
"set label ~s at ~{~f~^, ~}~%"
499 (cadr label
) (cddr label
)))))
500 ;; identifier for missing data
501 (format dest
"set datafile missing ~s~%" *missing-data-indicator
*))))
502 ;;returns a list with the name of the file created, or nil
503 (if (null (second terminal-file
))
504 nil
(list (second terminal-file
)))))
506 (defmethod plot2d-command ((plot gnuplot-plot
) fun options range
)
507 ;; Compute points to plot for each element of FUN.
508 ;; If no plottable points are found, end with an error.
511 (mapcar #'(lambda (f) (cdr (draw2d f range options
))) (cdr fun
)))
512 (when (= (count-if #'(lambda (x) x
) points-lists
) 0)
513 (merror (intl:gettext
"plot2d: nothing to plot.~%")))
514 (let ((legends-new) (legends (getf options
'$legend
)))
515 (unless (null legends
)
516 (dotimes (i (length legends
))
517 (unless (null (cdr (nth i points-lists
)))
518 (push (nth i legends
) legends-new
)))
519 (setf (getf options
'$legend
) (reverse legends-new
))))
521 (slot-value plot
'data
)
524 (slot-value plot
'data
)
525 (with-output-to-string (st)
526 (unless (or (getf options
'$logy
)
527 (and (getf options
'$y
) (listp (getf options
'$y
))))
528 (let (x y ymin ymax
(xmin +most-negative-flonum
+)
529 (xmax +most-positive-flonum
+))
530 (when (getf options
'$x
)
531 (setq xmin
(first (getf options
'$x
)))
532 (setq xmax
(second (getf options
'$x
))))
533 (dolist (points-list points-lists
)
534 (dotimes (i (/ (length points-list
) 2))
535 (setq x
(nth (* i
2) points-list
))
536 (setq y
(nth (1+ (* i
2)) points-list
))
537 (when (and (numberp x
) (>= x xmin
) (<= x xmax
))
542 (when (< y ymin
) (setq ymin y
))
543 (when (> y ymax
) (setq ymax y
)))
545 (setq ymax ymin ymin y
)
549 (setq ymin ymax ymax y
)
552 (when (and (numberp ymin
) (numberp ymax
) (< ymin ymax
))
553 (psetq ymin
(- (* 1.05 ymin
) (* 0.05 ymax
))
554 ymax
(- (* 1.05 ymax
) (* 0.05 ymin
)))
555 (format st
"set yrange [~,,,,,,'eg: ~,,,,,,'eg]~%" ymin ymax
))))
556 ;; user's commands; may overule any of the previous settings
557 (when (and (getf options
'$gnuplot_postamble
)
558 (> (length (getf options
'$gnuplot_postamble
)) 0))
559 (format st
"~a~%" (getf options
'$gnuplot_postamble
)))
562 (when (getf options
'$x
)
563 (format st
" [~{~,,,,,,'eg~^ : ~}]" (getf options
'$x
)))
564 (when (getf options
'$y
)
565 (unless (getf options
'$x
)
567 (format st
" [~{~,,,,,,'eg~^ : ~}]" (getf options
'$y
)))
568 (let ((legend (getf options
'$legend
))
569 (colors (getf options
'$color
))
570 (types (getf options
'$point_type
))
571 (styles (getf options
'$style
))
572 (i 0) style plot-name
)
573 (unless (listp legend
) (setq legend
(list legend
)))
574 (unless (listp colors
) (setq colors
(list colors
)))
575 (unless (listp styles
) (setq styles
(list styles
)))
576 (loop for v in
(cdr fun
) for points-list in points-lists do
578 (when ($listp
(car points-list
))
579 (dolist (level (cdar points-list
))
581 (setq style
(nth (mod i
(length styles
)) styles
))
583 (when ($listp style
) (setq style
(cdr style
)))
585 (setq plot-name
(ensure-string level
))
586 (when (> i
1) (format st
","))
588 (format st
" title ~s " plot-name
)
589 (format st
(gnuplot-curve-style style colors types i
)))
592 (setq style
(nth (mod i
(length styles
)) styles
))
594 (when ($listp style
) (setq style
(cdr style
)))
596 ;; label the expression according to the legend,
597 ;; unless it is "false" or there is only one expression
598 (if (member '$legend options
)
602 (nth (mod (- i
1) (length legend
)) legend
)) nil
))
603 (if (= 2 (length fun
))
608 (with-output-to-string (pn)
609 (cond ((atom v
) (format pn
"~a" ($sconcat v
)))
610 ((eq (second v
) '$parametric
)
613 ($sconcat
(fourth v
))))
614 ((eq (second v
) '$discrete
)
615 (format pn
"discrete~a" i
))
616 (t (format pn
"~a" ($sconcat v
))))))
617 (when (> (length plot-name
) 50)
618 (setq plot-name
(format nil
"fun~a" i
))))))
619 (when (> i
1) (format st
","))
622 (format st
" title ~s " plot-name
)
623 (format st
" notitle "))
624 (format st
(gnuplot-curve-style style colors types i
)))))
625 ;; Parses points data
627 (let (in-discontinuity points
)
628 (loop for points-list in points-lists do
630 ;; case "contour" with several plots in one list
631 (when ($listp
(car points-list
))
632 (dolist (level (cdr points-list
))
633 (loop for
(v w
) on
(cdr level
) 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
))
646 (first (getf options
'$y
)))
647 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%"
648 (first (getf options
'$x
))
649 (first (getf options
'$y
))))
652 ;; other cases with only one plot per list
653 (loop for
(v w
) on points-list by
#'cddr do
654 (cond ((eq v
'moveto
)
655 ;; A blank line means a discontinuity
656 (if (null in-discontinuity
)
659 (setq in-discontinuity t
))))
661 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%" v w
)
663 (setq in-discontinuity nil
))))
664 (if (and (null points
)
665 (first (getf options
'$x
)) (first (getf options
'$y
)))
666 (format st
"~,,,,,,'eg ~,,,,,,'eg ~%"
667 (first (getf options
'$x
))
668 (first (getf options
'$y
)))))
669 (when points-list
(format st
"e~%")))))))))
671 (defmethod plot3d-command ((plot gnuplot-plot
) functions options titles
)
672 (let ((i 0) fun xrange yrange lvars trans
(n (length functions
)))
674 (slot-value plot
'data
)
677 (slot-value plot
'data
)
678 (with-output-to-string ($pstream
)
679 ;; user's commands; may overule any of the previous settings
680 (when (and (getf options
'$gnuplot_postamble
)
681 (> (length (getf options
'$gnuplot_postamble
)) 0))
682 (format $pstream
"~a~%" (getf options
'$gnuplot_postamble
)))
683 ;; gnuplot command to produce the 3d plot
684 (format $pstream
"~a"
685 (gnuplot-plot3d-command "-" (getf options
'$palette
)
686 (getf options
'$gnuplot_curve_styles
)
687 (getf options
'$color
)
689 ;; generate the mesh points for each surface in the functions stack
690 (dolist (f functions
)
693 (setq xrange
(second f
))
694 (setq yrange
(third f
))
698 ($make_transform
`((mlist) ,(second xrange
)
700 (second fun
) (third fun
) (fourth fun
)))
701 (setq fun
'$zero_fun
))
707 (xmid (+ x0
(/ (- x1 x0
) 2)))
708 (ymid (+ y0
(/ (- y1 y0
) 2))))
709 (setq lvars
`((mlist) ,(second xrange
) ,(second yrange
)))
710 (setq fun
(coerce-float-fun fun lvars
"plot3d"))
711 ;; Evaluate FUN at the middle point of the range.
712 ;; Looking at a single point is somewhat unreliable.
713 ;; Call FUN with numerical arguments (symbolic arguments may
714 ;; fail due to trouble computing real/imaginary parts for
715 ;; complicated expressions, or it may be a numerical function)
716 (when (cdr ($listofvars
(mfuncall fun xmid ymid
)))
719 "plot3d: expected <expr. of v1 and v2>, [v1,min,max], [v2,min,max]~%"))
722 "plot3d: keep going and hope for the best.~%")))))
725 fun
(third xrange
) (fourth xrange
) (third yrange
)
726 (fourth yrange
) (first (getf options
'$grid
))
727 (second (getf options
'$grid
))))
728 (ar (polygon-pts pl
)))
729 (declare (type (cl:array t
) ar
))
730 (when trans
(mfuncall trans ar
))
731 (when (getf options
'$transform_xy
)
732 (mfuncall (getf options
'$transform_xy
) ar
))
733 (output-points pl
(first (getf options
'$grid
)))
734 (format $pstream
"e~%"))))))))
736 (defmethod plot-shipout ((plot gnuplot-plot
) options
&optional output-file
)
737 (case (getf options
'$plot_format
)
739 (let ((file (plot-set-gnuplot-script-file-name options
)))
741 #+sbcl
(sb-ext:native-namestring file
)
743 :direction
:output
:if-exists
:supersede
)
744 (format fl
"~a" (slot-value plot
'data
)))
745 (gnuplot-process options file output-file
)
746 (cons '(mlist) (cons file output-file
))))
748 (send-gnuplot-command (slot-value plot
'data
))
750 (send-gnuplot-command "unset output")
751 (cons '(mlist) output-file
)))))