Log in

No account? Create an account

cl-plplot for simple x-y plots from Lisp - Liam Healy

May. 5th, 2013

10:14 pm - cl-plplot for simple x-y plots from Lisp

Previous Entry Share

I had need recently to create quick x-y plots on the screen. I have been generating TikZ and PGFplots from Lisp for journal and conference papers and presentations. They look very nice but are not quick, either in writing or displaying. There are a number of CL packages listed for plotting, and I gave cl-plplot a try. It is in quicklisp. Though there is no active work on it, I was able to generate plots that I wanted quickly.

Here is an example, plotting the square function. First load needed libraries

sudo aptitude install libplplot-dev plplot11-driver-cairo plplot11-driver-xwin

(in Ubuntu 12.04LTS), then in Lisp
(ql:quickload :cl-plplot)
(defun x-y-plot ()
       (let* ((x (iter (for x from 0.0 to 10.0 by 0.25) (collect x result-type vector)))
              (y (map 'vector (alexandria:rcurry 'expt 2) x))
              (p (cl-plplot:new-x-y-plot x y))
              (w (cl-plplot:basic-window :x-label "x" :y-label "x squared" :title "The square function")))
         (cl-plplot:add-plot-to-window w p)
         (cl-plplot:render w "xwin")))

Click on window and Enter to close; clicking on the close box for the window causes Lisp to crash, at least from Slime.