# HG changeset patch # User Steve Losh # Date 1621833506 14400 # Node ID 8a7cfee11dea759878b0131940be5c06714a6721 # Parent 6740e201e636ae6d14736a58b9ed5c72332b58a8 Add `plot` convenience wrapper diff -r 6740e201e636 -r 8a7cfee11dea src/gnuplot.lisp --- a/src/gnuplot.lisp Mon May 24 01:05:42 2021 -0400 +++ b/src/gnuplot.lisp Mon May 24 01:18:26 2021 -0400 @@ -4,17 +4,13 @@ ;;; Action (second edition) specifically the section "Thought for the design of ;;; a gnuplot access layer" on page 253. + +;;;; State -------------------------------------------------------------------- (defparameter *gnuplot-path* "gnuplot") (defparameter *gnuplot-process* nil) -(defmacro with-gnuplot (options &body body) - (assert (null options)) - `(let ((*gnuplot-process* - (external-program:start *gnuplot-path* '() :input :stream :output t))) - (unwind-protect (progn ,@body *gnuplot-process*) - (close (external-program:process-input-stream *gnuplot-process*))))) - +;;;; Data Printers ------------------------------------------------------------ (defun gnuplot-data-sequence% (sequence s) (map nil (lambda (row) (map nil (lambda (val) @@ -39,6 +35,15 @@ (princ #\tab s)) (terpri s)))) + +;;;; Basic API ---------------------------------------------------------------- +(defmacro with-gnuplot (options &body body) + (assert (null options)) + `(let ((*gnuplot-process* + (external-program:start *gnuplot-path* '() :input :stream :output t))) + (unwind-protect (progn ,@body *gnuplot-process*) + (close (external-program:process-input-stream *gnuplot-process*))))) + (defun gnuplot-data (identifier data &aux (s (external-program:process-input-stream *gnuplot-process*))) "Bind `identifier` to `data` inside the currently-running gnuplot process. @@ -99,3 +104,20 @@ (sequence (map nil #'gnuplot-command commands))))) +;;;; Convenience Wrappers ----------------------------------------------------- +(defun plot (data &key (style :linespoints) (file "plot.pdf")) + "Plot `data` with gnuplot. + + Convenience wrapper around the gnuplot functions. This is only intended for + REPL-driven experimentation — if you want any customization you should use the + gnuplot interface instead. + + " + (with-gnuplot () + (gnuplot-data "$data" data) + (gnuplot-format "set terminal pdfcairo size 10in, 8in + set output '~A' + plot $data using 1:2 with ~A" + file + (string-downcase (symbol-name style))))) + diff -r 6740e201e636 -r 8a7cfee11dea src/package.lisp --- a/src/package.lisp Mon May 24 01:05:42 2021 -0400 +++ b/src/package.lisp Mon May 24 01:18:26 2021 -0400 @@ -429,7 +429,8 @@ :with-gnuplot :gnuplot-data :gnuplot-command - :gnuplot-format)) + :gnuplot-format + :plot)) (defpackage :losh.weightlists (:use :cl :iterate :losh.base