Introduction
A plotting library for Lua based on IUP to create plots using the standard Lua Interpreter. The plots are managed by a separate thread and don't block the interpreter. It consists of a module that Lua can require to get the plotting API and separate file which is spawned as a separate thread that manages the plots so that it doesn't block the main Lua interpreter.Dependencies
The module is depended on:- Lua Socket - luasocket - used for communication between module and plot server thread
- Lua Low Level threads - llthreads - Used to create a separate thread to run the plot server
- IUP Lua - IUP _ used to create the plots and the windows
- lua-complex - Lua Complex numbers library - Used to handle complex numbers in bode plots
API
Here is the API to use the plotting libraryIncluding the library
The library is required by doing:The lp table has the following elements:
- _VERSION - contains the version number for lua-plot
- listPlots - this is a function without any arguments that lists out the plots currently maintained by the lua-plot module and also the list of plots the plot server has. NOTE The plot server may have additional plots since they are not garbage collected yet even when the plots are garbage collected in the script locally.
- plot - This function creates a plot. It needs a table to be passed to it containing the attributes as defined in iupPlot. It adds 1 attribute AXS_BOUNDS which can be a table array with index 1 = AXS_XMIN, 2 = AXS_YMIN, 3=AXS_XMAX, 4=AXS_YMAX. It returns the created plot object. The plot is not shown but is created.
- window - This function creates a window. It needs a table to be passed to it containing the window layout slots. It is a table array with n values where n is the number of rows needed in the window. Each value tells how many slots to be there in that row. So a table
{2,2}
creates a window with 2 rows with 2 slots each. The table can also have IUP dialog attributes as defined in iupPlot that will be applied to the dialog representing the window. It returns the created window object. - bodePlot - This function creates a bode plot. It needs a table argument with certain keys:
- func - This key should have a function value. This is a single parameter function of the complex frequency s and returns a complex number for every s complex frequency (
jw
) from which the magnitude and phase can be computed and then plotted. - ini - starting frequency in Hz of the plot (default = 0.01)
- finfreq - ending frequency of the plot (default = 1MHz)
- steps - number of steps per decade for the plot (default=50)
- Returns a bode plot table object
- func - This key should have a function value. This is a single parameter function of the complex frequency s and returns a complex number for every s complex frequency (
Plot Object
The plot object provides the following API:- AddSeries: Adds a series of points to the plot object. Call it like:
-
plot:AddSeries(xvalues,yvalues[,AttributesTable])
- or
-
plot:AddSeries(xyPoints[,AttributesTable])
- xvalues - array of x value points
- yvalues - array of y value points (same size as that of xvalues or extra are ignored)
- xyPoints - array of points where each point is a table containing an x,y value pair. So such a table is like
{ {x1,y1}, {x2,y2}, ...{xn,yn} }
- AttributesTable - is a list of attributes to set on the plot object. For all attributes refer to iupPlot
- returns true for success or nil and error message in case of error
-
- Show: Shows a plot on the screen. If it is already there then brings focus to it. Call it like:
-
plot:Show([AttributesTable])
- AttributesTable - is a list of attributes to set on the plot window. For all attributes refer to iupDialog
- returns true for success or nil and error message in case of error
-
- Redraw: Redraws the plot object. Call it like:
-
plot:Redraw()
- returns true for success or nil and error message in case of error
-
- Attributes: Sets up attributes of the plot object. Call it like:
-
plot:Attributes(AttributesTable)
- AttributesTable - is a list of attributes to set on the plot object. For all attributes refer to iupPlot
- returns true for success or nil and error message in case of error
-
Window Object
The window object provides the following API:- AddPlot: This function adds an existing plot object to the window object. Call it like:
-
window:AddPlot(plotObj,coordinate)
- plotObj - is the plot object that needs to be added
- coordinate - it is a table with 2 numbers specifying the slot coordinate where the plot needs to be placed. Example
{1,2}
specifies placing the plot in the 1st row second slot. Coordinates start from 1. The coordinate slot should exist and should be empty. - returns true for success or nil and error message in case of error
-
- Show: Shows a window on the screen. If it is already there then brings focus to it. Call it like:
-
window:Show()
- returns true for success or nil and error message in case of error
-
- ClearSlot: Clears a slot in the given window so that it becomes empty and a plot can be placed on it. It is only required if another plot was present there. Call it like:
-
window:ClearSlot(coordinate)
- coordinate - it is a table with 2 numbers specifying the slot coordinate where the plot needs to be placed. Example
{1,2}
specifies placing the plot in the 1st row second slot. Coordinates start from 1. The coordinate slot should exist and should be empty. - returns true for success or nil and error message in case of error
-
Bode Plot Object
The bode plot object consists of a table with the magnitude and phase plot objects. The table has the following members:- mag - magnitude plot object. Can use all the plot object API with this.
- phase - phase plot object. Can use all the plot object API with this.
- addplot - function to add another bode plot to the same bode plot object. Call it like:
-
bodePlot:addplot([func,legend])
- func - This is a function value. This is a single parameter function of the complex frequency s and returns a complex number for every s complex frequency (
jw
) from which the magnitude and phase can be computed and then plotted. - legend - is the legend name for the added plot
-