eazy-gnuplot 'gnuplot Cookbook' Companion

This document is a companion for eazy-gnuplot users going through the 'excellent book by Lee Philips 'gnuplot Cookbook' from Packt Publishing. I highly recommend buying this book.

In this companion we demonstrate implementing the plots in the book using eazy-gnuplot. Liberties have been taken plots may not follow exactly what is shown in the book (when they don't they follow the spirit).

This document is an iPython notebook if you want to run the note book on your system please clone cl-jupyter (https://github.com/fredokun/cl-jupyter.git) into your quicklisp/local-projects directory and follow the instructions in the README.md. Clone eazy-gnuplot or download this notebook at https://github.com/guicho271828/eazy-gnuplot/blob/master/docs/eazy-gnuplot-cookbook-companion.ipynb. Finally execute 'ipython3 notebook' from a shell in the same directory uazy-gnuplot-cookbook-companion.ipynb and openthe notebook in Jupyter web interface.

In [1]:
;png-from-file is used to display png files in cl-jupyter if not using cl-jupyter create a dummy function
(when (not (fboundp 'png-from-file)) (defun png-from-file (f) f))
Out[1]:
NIL
In [2]:
;We need a directory called images to store our plots make sure it exists
(ensure-directories-exist "images/")
Out[2]:
"images/"
In [3]:
(quicklisp-client:quickload '(:eazy-gnuplot :clml.statistics :clml.utility))
(use-package :eazy-gnuplot)
To load "eazy-gnuplot":
  Load 1 ASDF system:
    eazy-gnuplot

; Loading "eazy-gnuplot"
.
To load "clml.statistics":
  Load 1 ASDF system:
    clml.statistics

; Loading "clml.statistics"

To load "clml.utility":
  Load 1 ASDF system:
    clml.utility

; Loading "clml.utility"
....
Out[3]:
T

1 Plotting Curves Boxes Points and More

In [4]:
(defun function-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output)
    (func-plot "[-5:5] (sin(1/x) - cos(x))*erfc(x)"))
  output)
(png-from-file (function-plot "images/function-plot.png"))
Out[4]:
In [5]:
(defun seperate-y-axis-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :y2tics '(-100 10) :ytics
              :nomirror)
    (func-plot "sin(1/x) axis x1y1,100*cos(x) axis x1y2"))
  output)
(png-from-file (seperate-y-axis-plot "images/seperate-y-axis-plot.png"))
Out[5]:
In [6]:
(defun seperate-y-axis2-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :x2tics '(-20 2) :xtics
              :nomirror :xrange '("[-10:10]") :x2range '("[-20:10]") :samples
              100)
    (func-plot "sin(1/x) axis x1y1, 100*cos(x-1) axis x2y2"))
  output)
(png-from-file (seperate-y-axis2-plot "images/seperate-y-axis2-plot.png"))
Out[6]:
In [7]:
(defun scatter-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output)
    (plot
     (lambda ()
       (loop for p in (map 'list (lambda (x y) (list x y))
                           (clml.statistics:rand-n
                            (clml.statistics:chi-square-distribution 100) 300)
                           (clml.statistics:rand-n
                            (clml.statistics:chi-square-distribution 10) 300))
             do (format t "~&~{~a~^ ~}" p)))
     :with '(:points :pt 7)))
  output)
(png-from-file (scatter-plot "images/scatter-plot.png"))
Out[7]:
In [8]:
(defun plotting-boxes-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :style '(:fill :pattern))
    (func-plot "[-6:6] besj0(x) with boxes, sin(x) with boxes"))
  output)
(png-from-file (plotting-boxes-plot "images/plotting-boxes-plot.png"))
Out[8]:
In [9]:
(defun plotting-boxes-solid-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :style '(:fill :solid))
    (func-plot "[-6:6] besj0(x) with boxes, sin(x) with boxes"))
  output)
(png-from-file
 (plotting-boxes-solid-plot "images/plotting-boxes-solid-plot.png"))
Out[9]:
In [10]:
(defun plotting-circles-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output)
    (plot
     (lambda ()
       (loop for p in (map 'list (lambda (x y z) 
                                         (declare (ignorable z))
                                         (list x y))
                            (clml.statistics:rand-n
                              (clml.statistics:chi-square-distribution 100) 300)
                            (clml.statistics:rand-n
                              (clml.statistics:chi-square-distribution 10) 300)
                            (clml.statistics:rand-n
                              (clml.statistics:chi-square-distribution 1) 300))
             do (format t "~&~{~a~^ ~}" p)))
     :with '(:circles)))
  output)
(png-from-file (plotting-circles-plot "images/plotting-circles-plot.png"))
Out[10]:
In [11]:
(defun drawing-filled-curves-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output)
    (plot
     (lambda ()
       (loop for i from 0 upto 50
             do (format t "~&~a ~a" i (sin i))))
     :with '(:filledcurves :above :y1 = 0.07)))
  output)
(png-from-file
 (drawing-filled-curves-plot "images/drawing-filled-curves-plot.png"))
Out[11]:
In [12]:
(defun drawing-blue-filled-curves (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :style '(fill pattern 5))
    (plot
     (lambda ()
       (loop for i from 0 upto 50
             do (format t "~&~a ~a" i (sin i))))
     :lt '(rgb "blue") :with '(:filledcurves :above :y1 = 0.07)))
  output)
(png-from-file
 (drawing-blue-filled-curves "images/drawing-blue-filled-curves.png"))
Out[12]:

This exercise requires Gnuplot 5

In [13]:
(defun filled-curves-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(:pngcairo) :output output :style
              '(fill pattern 5))
    (plot
     (lambda ()
       (loop for i from 0 upto 1 by 0.1
             do (format t "~&~a ~a ~a" i i (sin i))))
     :using '(1 2 3) :lw (list 3 'notitle) :using '(1 2)
     :lw (list 3 'notitle) :using '(1 3) :lw '(3 notitle) :with
     '(:filledcurves :above :y1 = 0.07)))
  output)
(png-from-file (filled-curves-plot "images/filled-curves-plot.png"))
Out[13]:
In [14]:
(defun finance-bars-plot (output)
  (let ((s
         (clml.utility.data:fetch
          "http://www.google.com/finance/getprices?i=60&p=10d&f=d,o,h,l,c,v&df=cpct&q=ibm"
          :stream t)))
    (clml.utility.data:process-finance-header s)
    (with-plots (*standard-output* :debug nil)
      (gp-setup :terminal '(pngcairo) :output output :bars 2)
      (plot
       (lambda ()
         (loop for row across (subseq
                               (clml.utility.csv:read-csv-stream s :type-spec
                                                                 '(float float
                                                                         float
                                                                         float
                                                                         float
                                                                         float))
                               0 100)
               do (format t "~&~{~a~^ ~}" (coerce row 'list))))
       :using '(0 2 3 4 5) :with 'financebars)))
  output)
(png-from-file (finance-bars-plot "images/finance-bars-plot.png"))
Out[14]:
In [15]:
(defun histogram-like-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output)
    (plot
     (lambda ()
       (loop for i from -2 upto 2 by 0.1
             do (format t "~&~a ~a" i (- (* i i)))))
     :with '(:histeps)))
  output)
(png-from-file (histogram-like-plot "images/histogram-like-plot.png"))
Out[15]:
In [16]:
(defun histogram-stacked-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :terminal '(pngcairo) :output output :style
              '(fill solid 1.0 border lt -1) :style '(data histograms) :style
              '(histogram rowstacked))
    (plot
     (lambda ()
       (loop for i from -2 upto 2 by 0.1
             do (format t "~&~a ~a ~a ~a" i (- (* i i)) (* i i) i)))
     :using (list "(-$2)") :using (list "(20*$2)") :notitle nil))
  output)
(png-from-file (histogram-stacked-plot "images/histogram-stacked-plot.png"))
Out[16]:
In [17]:
(defun multiple-histograms-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title
              "multiple histograms" :key '(invert reverse left outside) :key
              '(autotitle columnheader) :style '(data histogram) :style
              '(histogram rowstacked) :style '(fill solid border -1))
    (plot
     (lambda ()
       (loop for r in '((8.01 1 5 1) (8.02 3 5 1) (8.03 4 4 1) (8.04 3 4 1)
                        (8.05 1 2 1))
             do (format t "~&~{~^~a ~}" r)))
     :using '(2 "xtic(1)") :title "col0" :using 2 :title "col1" :using 3 :title
     "col2" :using 4 :title "col3"))
  output)
(png-from-file
 (multiple-histograms-plot "images/multiple-histograms-plot.png"))
Out[17]:
In [18]:
(defun error-bars-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "error bars" :bars 3
              :pointsize 3 :xrange (list "[ 8.0:8.06]"))
    (plot
     (lambda ()
       (loop for r in '((8.01 1 0.2) (8.02 3 0.3) (8.03 4 0.4) (8.04 3 0.4)
                        (8.05 1 0.1))
             do (format t "~&~{~^~a ~}" r)))
     :using "1:(-$2):3 with errorbars" :title "error" :using "1:(-$2):3 pt 7"
     :title "col0"))
  output)
(png-from-file (error-bars-plot "images/error-bars-plot.png"))
Out[18]:
In [19]:
(defun boxerror-bars-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "error bars" :style
              '(fill pattern 2 border) :xrange (list "[8.0:8.06]"))
    (plot
     (lambda ()
       (loop for r in '((8.01 1 0.2) (8.02 3 0.3) (8.03 4 0.4) (8.04 3 0.4)
                        (8.05 1 0.1))
             do (format t "~&~{~^~a ~}" r)))
     :using "1:($2):3 with boxerrorbars" :title "boxers"))
  output)
(png-from-file (boxerror-bars-plot "images/boxerror-bars-plot.png"))
Out[19]:
In [20]:
(defun candlestick-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "candlestick"
              :xrange (list "[0:5]") :yrange (list "[0:25]") :boxwidth
              '(0.2 absolute))
    (plot
     (lambda ()
       (loop for r in '((1 5 7 10 15 24 0.3 "'quick'")
                        (2 6 8 11 16 23 0.4 "'fox'")
                        (3 5 7 11 17 22 0.5 "'lazy'")
                        (4 6 9 10 18 21 0.3 "'dog'"))
             do (format t "~&~{~^~a ~}" r)))
     :using
     (list
      "1:3:2:6:5:xticlabels(8) with candlesticks title 'quartiles' whiskerbars")
     :using (list "1:4:4:4:4 with candlesticks lt -1 notitle")))
  output)
(png-from-file (candlestick-plot "images/candlestick-plot.png"))
Out[20]:
In [21]:
(defun impluse-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "candlestick"
              :xrange (list "[0:5]"))
    (plot
     (lambda ()
       (loop for r in '((1 5 7 10 15 24 0.3 "'quick'")
                        (2 6 8 11 16 23 0.4 "'fox'")
                        (3 5 7 11 17 22 0.5 "'lazy'")
                        (4 6 9 10 18 21 0.3 "'dog'"))
             do (format t "~&~{~^~a ~}" r)))
     :using (list "1:6:xticlabels(8) with impulses")))
  output)
(png-from-file (impluse-plot "images/impluse-plot.png"))
Out[21]:
In [22]:
(defun stem-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "candlestick"
              :xrange (list "[0:5]") :yrange (list "[0:25]"))
    (plot
     (lambda ()
       (loop for r in '((1 5 7 10 15 24 0.3 "'quick'")
                        (2 6 8 11 16 23 0.4 "'fox'")
                        (3 5 7 11 17 22 0.5 "'lazy'")
                        (4 6 9 10 18 21 0.3 "'dog'"))
             do (format t "~&~{~^~a ~}" r)))
     :using '(1 6 "xticlabels(8)") :with '(impulses lw 4 title "counts") :using
     '(1 6) :with '(points pt 9 notitle)))
  output)
(png-from-file (stem-plot "images/stem-plot.png"))
Out[22]:
In [23]:
(defun parametric-plot (output)
  (with-plots (*standard-output* :debug nil)
    (gp-setup :output output :terminal '(:pngcairo) :title "parametric"
              :samples 1000 :parametric 'nil :trange (list "[-7:20]"))
    (func-plot "sin(7*t), sin(111*t)" :notitle 'nil))
  output)
(png-from-file (parametric-plot "images/parametric-plot.png"))
Out[23]: