API for package: vgplot
# vgplot

This common lisp library is an interface to the gnuplot plotting
utility.

The intention of the API is to resemble to some of the plot commands
of octave or matlab.

## Usage

(asdf:load-system :vgplot) or (ql:quickload :vgplot)

(vgplot:plot '(1 2 3) '(0 -2 17))

For examples run the demo function:

(vgplot:demo)

and see API documentation in docs/vgplot.html or on http://volkers.github.io/vgplot/vgplot.html

## License

Copyright (C) 2013 - 2021 Volker Sarodnick

mail: (remove #\y (subseq "avoidspamyvolykerysyar@gymyx.net" 9))

GNU General Public License
-Variables
*debug*: NIL variable
Actvate debugging when true.
*gnuplot-binary*: "gnuplot" variable
Gnuplot binary. Change when gnuplot not in path.
-Functions
3d-plot &rest vals function
Do a 3d plot.  Uses gnuplot's 'splot'.
     The inputs are similar to 'plot', but with some key differences:
vals could be: x y z
               x y z label-string
style commands in the label-string work the same as in 'plot'.
axis &optional limit-list compiled-function
Set axis to limit-list and return actual limit-list, limit-list could be:
'(xmin xmax) or '(xmin xmax ymin ymax),
values can be:
  a number: use it as the corresponding limit
  nil:      do not change this limit
  t:        autoscale this limit
without limit-list do return current axis.
bar &key x y (style grouped) (width 0.8) (gap 2.0) function
Create a bar plot y = f(x) on active plot, create plot if needed.
                :x     (optional) vector or list of x strings or numbers
                       plot to index if not provided
                :y     list of y '((y &key :label :color) (y &key :label :color) ...)
                       y      vector or list of y values
                       :label string for legend label (optional)
                       :color string defining the color (optional);
                              must be known by gnuplot, e.g. blue, green, red or cyan
                :style (optional) "grouped" (default) or "stacked"
                :width (optional) width of the bars where 1.0 means to fill the space completely
                       (for the gap in style "grouped" see parameter gap)
                :gap   (optional, only used in style "grouped") the gap between the groups
                       in units of width of one boxwidth
e.g.:
   (bar :x #("Item 1" "Item 2" "Item 3")
        :y '((#(0.3 0.2 0.1) :label "Values" :color "blue")
             (#(0.1 0.2 0.3) :label "Values" :color "red"))
        :style "stacked"
        :width 0.6)
close-all-plots function
Close all connected gnuplots
close-plot function
Close connected gnuplot
demo compiled-function
Show usecases of vgplot.
figure function
Add a new plot window to a current one.
format-plot print? text &rest args function
Send a command directly to active gnuplot process, return gnuplots response
print also response to stdout if print? is true
grid style &key (replot t) compiled-function
Add grid to plot if style t, otherwise remove grid.
If key parameter replot is true (default) run an additional replot thereafter.
legend &rest options compiled-function
Provide options to the legend aka keys.
     :show       Show legend (default)
     :hide       Hide legend
     :boxon      Use box around the legend
     :boxoff     Don't use a box (default)
     :left       Title left of sample line (default)
     :right      Title right of sample line
     :north      Place legend center top
     :south      Center bottom
     :east       Right center
     :west       Left center
     :northeast  Right top (default)
     :northwest  Left top
     :southeast  Right bottom
     :southwest  Left bottom
     :at x y     Place legend at position x,y
     :inside     Place legend inside the plot (default)
     :outside    Place legend outside the plot
load-data-file fname compiled-function
Return a list of found vectors (one vector for one column) in data file fname
(e.g. a csv-file).
Datafile fname must hold columns separated by spaces, tabs or commas (other separators may work),
content after # till end of line is assumed to be a comment and ignored.
loglog &rest vals function
Produce a two-dimensional plot using logarithmic scales scale for both axis.
See the documentation of the plot command for a description of the arguments.
meshgrid-map fun xx yy compiled-function
Helper function for a surface plot (surf).
Map fun to every pair of elements of the arrays xx and yy and return the corresponding array zz.
See surf for an example.
meshgrid-x x y compiled-function
Helper function for a surface plot (surf).
Given vectors of X and Y coordinates, return array XX for a 2-D grid.
The columns of XX are copies of X.
Usually used in combination with meshgrid-y.
See surf for examples.
meshgrid-y x y compiled-function
Helper function for a surface plot (surf).
Given vectors of X and Y coordinates, return array YY for a 2-D grid.
The rows of YY are copies of Y.
Usually used in combination with meshgrid-x.
See surf for examples.
new-plot function
Add a new plot window to a current one.
plot &rest vals function
Plot y = f(x) on active plot, create plot if needed.
vals could be: y                  plot y over its index
               y label-string     plot y over its index using label-string as label
               x y                plot y = f(x)
               x y label-string   plot y = f(x) using label-string as label
               following parameters add curves to same plot e.g.:
               x y label x1 y1 label1 ...
label:
A simple label in form of "text" is printed directly.

A label with style commands: label in form "styles;text;add-styles":

In the ordinary case add-styles will be empty.

styles can be (combinations possible):
   "-" lines
   ":" dotted lines
   "." dots
   "+" points
   "o" circles
   "r" red
   "g" green
   "b" blue
   "c" cyan
   "k" black
   "y" yellow
   "m" magenta
   "w" white
   "#RRGGBB" sets an arbitrary 24-bit RGB color (have to be exactly 6 digits)

e.g.:
   (plot x y "r+;red values;") plots y = f(x) as red points with the
                                 label "red values"

If add-styles is not empty, the string in add-styles is send unmodyfied to gnuplot, other styles and colors are skipped.
This is useful to set more complicated styles, e.g.:

   (plot x y ";use of additional styles;with linespoints pt 7 ps 2 lc 'red'")
                                 plots y = f(x) as lines with red points, pointsize 2 with the
                                 label "use of additional styles"

To use a backslash in add-styles you have to quote it, e.g.:

   (plot x y ";;with points pt '\U+2299'")
plot-file data-file &key (x-col) function
Plot data-file directly, datafile must hold columns separated by spaces, tabs or commas
(other separators may work), use with-lines style.
                :x-col     (optional) column to use as x values.
                           plot to index if not provided
print-plot filename &key terminal function
Print the actual plot into filename (a pathname).
Use the (optional) terminal or if not provided,
use the extension of filename to guess the terminal type.
Guessing of terminals works currently for: gif, pdf, png

Examples: (vgplot:print-plot #p"plot.pdf")
          (vgplot:print-plot #p"plot.eps" :terminal "epscairo")

It is possible to give additional parameters inside the terminal parameter, e.g.:
(vgplot:print-plot #p"plot.pdf" :terminal "pdfcairo size \"5cm\",\"5cm\"")
range a &optional b (step 1) compiled-function
Return vector of values in a certain range:
(range limit) return natural numbers below limit
(range start limit) return ordinary numbers starting with start below limit
(range start limit step) return numbers starting with start, successively adding step untill reaching limit (excluding)
replot compiled-function
Send the replot command to gnuplot, i.e. apply all recent changes in the plot.
semilogx &rest vals function
Produce a two-dimensional plot using a logarithmic scale for the X axis.
See the documentation of the plot command for a description of the arguments.
semilogy &rest vals function
Produce a two-dimensional plot using a logarithmic scale for the Y axis.
See the documentation of the plot command for a description of the arguments.
stairs &rest vals compiled-function
Produce a stairstep plot.
vals could be: y                  plot y over its index
               x y                plot y = f(x)
               x y label-string   plot y = f(x) using label-string as label
               following parameters add curves to same plot e.g.:
               x y label x1 y1 label1 ...

For the syntax of label-string see documentation of plot command.

If you only want to prepare the sequences for later plot, see
function stairs-no-plot.
stairs-no-plot yx &optional y compiled-function
Prepare a stairstep plot, but don't actually plot it.
Return a list of 2 sequences, x and y, usable for the later plot.

If one argument is given use it as y sequence, there x are the indices.
If both arguments are given use yx as x and y is y.

If you want to plot the stairplot directly, see function stairs.
subplot rows cols index function
Set up a plot grid with rows by cols subwindows and use location index for next plot command.
The plot index runs row-wise.  First all the columns in a row are
filled and then the next row is filled.

For example, a plot with 2 rows by 3 cols will have following plot indices:

          +-----+-----+-----+
          |  0  |  1  |  2  |
          +-----+-----+-----+
          |  3  |  4  |  5  |
          +-----+-----+-----+

Observe, gnuplot doesn't allow interactive mouse commands in multiplot mode.
surf &rest vals function
Plot a 3-D surface mesh.
Vals could be: zz [label-string]
               xx yy zz [label-string]

For label-string see documentation of plot.

xx, yy and zz are 2 dimensional arrays usually produced by meshgrid-x, meshgrid-y and meshgrid-map.
All 3 arrays have to have the same form where the rows follow the x direction and the columns the y.
xx: #2A((x0  x0  x0  ... x0)
        (x1  x1  x1  ... x1)
        ...
        (xn  xn  xn  ... xn))
yy: #2A((y0  y1  y2  ... ym)
        (y0  y1  y2  ... ym)
        ...
        (y0  y1  y2  ... ym))
zz: #2A((z00 z01 z02 ... z0m)
        (z10 z11 z12 ... z1m)
        ...
        (zn0 zn1 zn2 ... znm))

Example 1: Plot some measurement data without providing xx and yy:
  (let ((zz (make-array (list 3 4) :initial-contents '((0.8 1.5 1.7 2.8) (1.8 1.2 1.2 2.1) (1.7 1.0 1.0 1.9)))))
     (vgplot:surf zz "r;array plotted without providing xx or yy;"))

Example 2: Plot some measurement data:
  (let* ((x #(1.0 2.0 3.0))
         (y #(0.0 2.0 4.0 6.0))
         (zz (make-array (list (length x) (length y)) :initial-contents '((0.8 1.5 1.7 2.8) (1.8 1.2 1.2 2.1) (1.7 1.0 1.0 1.9))))
         (xx (vgplot:meshgrid-x x y))
         (yy (vgplot:meshgrid-y x y)))
     (vgplot:surf xx yy zz))

Example 3: Plot a function z = f(x,y), e.g. the sombrero function:
  (let* ((eps double-float-epsilon)
         (fun #'(lambda (x y) (/ (sin (sqrt (+ (* x x) (* y y) eps))) (sqrt (+ (* x x) (* y y) eps)))))
         (x (vgplot:range -8 8 0.2))
         (y (vgplot:range -8 8 0.2))
         (xx (vgplot:meshgrid-x x y))
         (yy (vgplot:meshgrid-y x y))
         (zz (vgplot:meshgrid-map fun xx yy)))
    (vgplot:surf xx yy zz)
    (vgplot:format-plot nil "set hidden3d")
    (vgplot:format-plot nil "set pm3d")
    (vgplot:replot))
text x y text-string &key (tag) (horizontalalignment left) (rotation 0) (font) (fontsize) (color ) compiled-function
Add text label text-string at position x,y
optional:
   :tag nr              label number specifying which text label to modify
                        (integer you get when running (text-show-label))
   :horizontalalignment "left"(default), "center" or "right"
   :rotation degree     rotate text by this angle in degrees (default 0) [if the terminal can do so]
   :font "" use this font, e.g. :font "Times" [terminal depending, gnuplot help
                        recommends: http://fontconfig.org/fontconfig-user.html for more information]
   :fontsize nr
   :color "color"     one of red, green, blue, cyan, black, yellow or white
                        an unrecogniced color is send unchanged to gnuplot, this can be used to get other colors or effects, e.g:
                        "tc rgb '#112233'"  gives color with the RGB code 0x112233 (0xRRGGBB)
                        "tc lt 1" gives the same color as line 1

Observe, it could alter the font of the labels (aka legend or key in
gnuplot terms) if you change font or fontsize of a text field. To
explicitly chose fontsize (or font) for the label you could use:

(format-plot t "set key font \",10\"")
(replot)
text-delete &rest tags compiled-function
Delete text labels specified by tags.
A tag is the number of the text label you get when running (text-show-label).
text-show-label compiled-function
Show text labels. This is useful to get the tag number for (text-delete)
title str &key (replot t) compiled-function
Add title str to plot. If key parameter replot is true (default)
run an additional replot thereafter.
xlabel str &key (replot t) compiled-function
Add x axis label. If key parameter replot is true (default)
run an additional replot thereafter.
ylabel str &key (replot t) compiled-function
Add y axis label. If key parameter replot is true (default)
run an additional replot thereafter.
zlabel str &key (replot t) compiled-function
Add z axis label. If key parameter replot is true (default)
run an additional replot thereafter.