Programming Environment for Numerical Computing

XY Plotting

The plot namespace includes functions to plot data:

p = plot.new(width, height, template) Create plot. Returns the plot identifier,
    or nil if error
plot.add(p, x, y) add a curve with tables x,y
plot.add(p, x, y, ey) adds a curve with tables x,y and error bar table ey
plot.add(p, x, y, ex, ey) adds a curve with tables x,y and error bar tables ex,ey
plot.add(p, curve, x, y) add tables x,y to an existing curve
plot.add(p, curve, x, y, ey) adds tables x,y to an existing curve, with error bar table ey
plot.add(p, curve, x, y, ex, ey) adds tables x,y to an existing curve, with error bar tables ex,ey
plot.add(p, expr, npoints, xstart, xend) add a curve with:
expression (like "sin(x)", "1 - exp(-x)" …)
number of points npoints
interval from xstart to xend
plot.add(p, fname) add a curve from data file (columns 1 and 2, TAB separated)
plot.set(p, curve, prop, val) set the curve properties. prop can be:
"size" for curve line and symbol size.
    val is the size ("1", "2" ...)
"style" for curve style.
    val is "o" for circle, "+" for plus sign,
    "s" for square, "d" for diamond,
    and "-" for line.
    Example: "-s" for line and squared markers.
"legend" for curve legend.
    val is the legend text.
"color" for curve color.
    val is color name like "red", "blue", ...
    or hex-value like "FF0000")
plot.set(p, prop, val) set the plot properties. prop can be:
"title" for the plot window title.
  val is the window title.
"xlabel" for the bottom axis label.
  val is the axis label.
"ylabel" for the left axis label.
  val is the axis label.
"xscale" for the bottom axis scale:
  val is "log" or "linear".
"yscale" for the left axis scale:
  val is "log" or "linear".
"xlim" for the bottom axis scale:
  val is "[min,max]" where min and max are the x-axis limits.
"ylim" for the left axis scale:
  val is "[min,max]" where min and max are the y-axis limits.
"maxpoints" set val as the maximum number of points per curve.
"autolim". val is "true": automatically set the axis limits.
plot.save(p, fname) save plot to file (SVG vectorial or PNG raster image)
plot.update(p) update the plot window
plot.close(p) close the plot window

Example #1:

Copy the following script in the editor and click Run (or press F12 on Windows and Linux)

-- Plot
x = {0,1,2,3,4,5}
y = {0,1,2,3,4,5}
-- create a plot
p = plot.new(800, 600)
plot.set(p, "title", "Plot example")
-- add curve to the new plot
plot.add(p, x, y)
-- set curve #1 line size
plot.set(p, 1, "size", 2)
-- set curve #1 style(line and marker)
plot.set(p, 1, "style", "-o")
plot.set(p, 1, "color", "0000FF")
plot.update(p)

Example #2:

Copy the following script in the editor and click Run (or press F12 on Windows and Linux)

-- Real-time plot

x = {}
y = {}
x[1] = 1
-- random number between 0 and 1
y[1] = math.random()

--create a plot
p = plot.new()
--add curve to the new plot
plot.add(p, x, y)
--set curve #1 line size
plot.set(p, 1, "size", 2)
--set curve #1 style (line and marker)
plot.set(p, 1, "style", "-o")
--set the maximum number of points
plot.set(p, "maxpoints", 21)
plot.update(p)

-- the total number of points (only the last 21 points will be displayed)
n = 100
-- update the curve every 200 milliseconds
for i = 2, n, 1 do
     x[1] = i
     -- random number between 0 and 1
     y[1] = math.random()
     -- add new data to curve #1
     plot.add(p, 1, x, y)
     -- sleep during 200 milliseconds
     time.sleep(200)
end

All the plot properties (curves options, scale, axis, colors …) can be easily modified in the plot window. All these properties can be saved in a style template file to be used later. One can create a style template for a particular plot type and use it in the Lua code (plot.new function). You can also add text, lines, rectangles, ellipses … to the plot and saved it as PNG or SVG directly from within the plot window.
 Copyright(C) 2010-2017 Dr. Sidi HAMADY