One of the most useful commands is **plot**. With this command you can output virtually any graph, including bar graphs and pie charts.

For those not well versed in Lua here is a crash course in the language.

In its simplest form plot takes a list of numbers and makes a graph with the x-axis from 0 to 1. So the script

** plot({3,1,2,4,0})**

produces the following output:

This is interesting but will not take us too far.

A more general plot command takes a list which contains two parameters. An example is:

** plot({x={2,3,5,7,11}, y={1,2,4,-3,2}})**

The x parameter takes points on the horizontal axis and the y parameter takes points on the vertical axis. The output here is

This plot command allows the x-axis numbers to not be monotonic, so a command like **plot({x={0,1,1,0},y={0,0,1,1}}) **will happily draw a box.

At this stage we draw lots of different types of graphs, but we are still using default parameter. In this app we can control nearly every aspect of a graph. The first thing we’ll learn to change is the color. In the table we can add a parameter c with one integer value ranging from 1 to 14. So **plot({x={0,1,1,0},y={0,0,1,1}, c=2}) **displays the above plot in green.

The full list of colors is

- red
- green
- blue
- cyan
- yellow
- white
- light gray
- gray
- dark gray
- black
- magenta
- orange
- purple
- brown

Another aspect we can control is the line width with the parameter **w**. By default w is 1. So adding w=2 will produce a line twice as thick. Setting w to 0 completely hides the line, this is useful in constructing scatterplots.

The next useful item to change is the plot marker. Its size is controlled by the parameter r. By default r is set to 1. The following plot hides shows the effect of r (and hiding the plot line).

** plot( {x={2,3,5,7,11,13}, y={1,2,4,-3,2,-4},r=8,w=0} )**

Its output is

Another aspect we are able to control is the shape of the marker. This is via the parameter **s**. By default s=1, the ellipse. The full list of shapes available are:

- ellipse
- triangle
- rectangle
- pentagon
- hexagon
- diamond
- star
- snowflake
- dash
- plus
- cross

Also, the parameter ss controls the ratio of horizontal vs vertical size of the marker. Normally ss=1, making the ellipse look like a circle and the rectangle a square. Changing this parameter allows for more shapes. The parameter sc controls the color of the marker, which by default is the same color as the lines in the graph. sb controls the thickness of the border on the marker, by default it is 0.5.

So far we have done only one plot. Multiple plots on the same axes are a very easy extension of the single plot case. The plot command can take arbitrarily many parameters, where each is a list as described above. Each of these will be added to the same graph. Let’s make one now.

** plot( {x={1,2,3,4,5},y={3,2,1,2,-3},r=5,sc=3}, {x={2,4,6,8},y={1,0,-1,-2},s=3,sc=5,r=5} )**

Here is the output:

QuantLua is smart enough to select different colors for different plots, but we can override this.

So far we have been plotting lists of numbers, so let’s get a useful list of numbers to plot. The QuantLua function **getprices(“goog”) **will return google’s stock prices from 2011-01-01 by default. (We’ll cover getprices and its friends in another posting)

** d=getprices(“goog”)**

returns a table with several parameters. The parameter **d.values** is the list of stock prices and **d.dates **is the list of the corresponding dates in numeric form. We want to plot this date so let’s try the following script:

** d=getprices(“goog”)**

** plot({x=d.dates,y=d.values})**

This almost works except that QuantLua has no way of know that the x-axis is dates, not numbers. To fix this we include the parameter dates=1. The second line now looks like this:

**plot({x=d.dates,y=d.values, dates=1})**

The resulting graph is:

So far we have been plotting list, which are by far the most common. We can also plot functions very easily. Suppose we have a one variable function defined as

** function g(x)**

** return 2*math.sin(x)+x**

** end**

This function can be plotted with the command

** plot({f=g, x={-16,16}})**

giving the following plot:

In conclusion, here is the summary about plot.

It takes three forms:

**plot(**most basic plot, very little control*lst*)**plot({x=**this is the most general plot allowing for fine control of the parameters*lst1*,y=*lst2 [,optional parameters]*})**plot({f=**enable plotting functions.*function*,x=*range**[,optional parameters]*})

Several of these plots can be put within one plot statement, enabling multiple graphs on one set of axes.

The optional parameters are

**c**determines color of plot line, default varies.**w**determines line thickness, default is 1.**r**determines size of marker, default is 0.5.**s**determines marker shape, default is ellipse.**ss**marker shape scale, default is 1.**sc**marker color, default is c.**sb**marker’s black boundary, default is c.**p**used in function plots, numper of points selected in the domain, default 100.**dates**, determines if x-axis values are date or numbers, default 0.**axis**, determines if axes are drawn or not, default 1.**n**, plot legend name, default “”**title**, plot title, default “”**xtitle**, label on x-axis.**ytitle**, label on y-axis.