8a7cfee11dea

Add `plot` convenience wrapper
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 24 May 2021 01:18:26 -0400
parents 6740e201e636
children eb9bf5de0279
branches/tags (none)
files src/gnuplot.lisp src/package.lisp

Changes

--- 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)))))
+
--- 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