2D plot

figure is a class. To make a 2D plot, you first create a member of the this class. It is like a blank canvas and you can add graphical elements such as curves, lines, symbols, etc, to it using functions defined for figure class. When everything is added to the figure, it can be saved in a postscript file, which can then be displayed or printed.

  1. Create a figure.

    Use the following command to create a figure and assign it to a variable named "fig":

    fig = figure.new();
    

  2. Plot data.

    plot is an attribute function of figure and can treat two vectors x and y as coordinates of the locations of a sequence of points in the 2d plane. Marks can be drawn at these locations, or lines and curves drawn through them. The command for plotting x and y data on figure fig is

    fig.plot(x, y, linetype, linewidth, color, gray);
    
    where x and y are two vectors of the same length. The plot can be straight line segements joining the points, marks placed at the locations defined by x and y, or a smooth curve (a piece-wise cubic polynomial) through the dots. The type of the plot is specified by the argument linetype.

    solid -- dotty ....
    dash - - - - dashdot --.--.--.
    dot . . . . circle
    oval square
    diamond cross
    flake asterisk
    triangle dtriangle
    plus +++ curve (smooth curve)

    linewidth is a number. The line to be drawn will be linewidth times as wide as the default linewidth. For example, to make the line twice as wide as the normal, use value 2.

    color is a vector of three numbers between 0 and 1, with each component representing the intensity of red, green, or blue. gray is a number between 0 and 1.

    It's not necessary to provide all the arguments to plot. The default values of linetype, linewidth, color, and gray are "solid", 1, [0, 0, 0], and 1. Therefore a simple call

    fig.plot(x, y);
    
    will draw solid straight line segments through the (x, y) points; the width of the lines is 1 unit, and the color is black. This is because that when calling a function, you may provide only the first few arguments, the others will receive default values. For example,

    xx = [0, 1, 2, 3, 4, 5];
    yy = [0, 1, 0.5, 0.75, 0.625, 0.6875];
    fig.plot(x, y);
    

    To change the line type,

    fig.plot(xx, yy, "oval");
    

    To change the line width as well

    fig.plot(xx, yy, "oval", 2);
    

    If you just want to specify the line width, you still have to provide at least the first four arguments, but you can use default value for the third argument linetype

    fig.plot(xx, yy, *, 2);
    

    Named argument By the previous way of calling fig.plot, the order of arguments have to match the signature of the function. Alternatively, you can use named arguments. For example

    fig.plot{x = xx, y = yy, linewidth = 2};
    
    Note that you can not only skip certain arguments, but don't have to follow any specific order of argument either. For example
    fig.plot{y = yy, linewidth = 2, x = xx};
    
    However you still need to provide the correct names of the arguments, namely, x, y, linewidth, etc.

    Example

    xx = [0, 1, 2, 3, 4, 5];
    yy = [0, 1, 0.5, 0.75, 0.625, 0.6875];
    fig = figure.new();
    fig.plot(xx, yy, *, *, colors.green);
    fig.plot(xx, yy*0.8, "curve", *, colors.purple);
    fig.plot(xx, yy*0.8, "oval");
    fig.plot{x = xx, y = yy*0.3, linetype = "curve", color = colors.red};
    fig.plot{x = xx, y = yy*0.3, linetype = "diamond", linewidth = 2, color = colors.olive};
    fig.save("symbols.eps");
    
    Image symbols

  3. Draw objects

    Some simple shapes can be drawn on the canvas without first defing data.

    Example

    fig = figure.new();
    fig.line([0, 0], [2, 2], "->");
    fig.ellipse{ox = 0, oy = 0, a = 2, b = 1.5, theta = 0, color = colors.red};
    fig.rectangle{x = 0, y = 0, width = 4, height = 3, color = colors.lime};
    fig.style = "empty";
    fig.save("objects.eps");
    
    Image objects

  4. modify figure attributes

    A figure has the following attributes that can be modified.

  5. Print text

    A character string can be printed anywhere in the graph. The location, size, and other attributes of the text can be specified, but it usually would take some trial and error to get it right or satisfactory. The following is the command for printing text

    fig.print(x, y, contents, orientation, alignment, color, font, fontstyle,
    fontscale);
    
    where x and y are the coordinates of the location of the text, contents is the text to be printed (a string of characters). The other values are as follows:

    argument allowed values
    orientation "portrait", "landscape"
    alignment "left", "right", "center", "top", "bottom",
    font "Helvetica", "Times", "Courier", "Symbol"
    fontstyle "Regular", "Bold", "Italic", "BoldItalic"
    fontscale positive number
    color vector of three number between 0 and 1

  6. Save a figure.

    You won't see any picture until you save the figure in a file. To save the figure as an encaspuslated postscript file, do fig.save(filename). For example

    fig.save("flowering-times.eps");
    
    Note that most of the processing occurs when the figure is being saved, therefore if there's something wrong with the plotting commands, error messages usually are not produced until at this stage.

    After a figure is saved, it still exists and more contents can be added to it and then it can be saved again.

  7. Reset a figure

    If you're not satisfied with how a figure looks, you can erase everything in it by using clear. To delete all elements in a figure and leave it empty

    fig.clear();
    
    Or you could simply redo fig = figure.new() -- the previous value of fig is destroyed.

  8. Working with color

    A color a vector of length three with each element (a number between 0 and 1) representing the intensity of red, green, or blue. Sixteen colors are predefined in the global variable colors.

    global.colors = {
         black = [0, 0, 0],
         gray = [0.5, 0.5, 0.5],
         silver = [0.75, 0.75, 0.75],
         white = [1, 1, 1],
         yellow = [1, 1, 0],
         lime = [0, 1, 0],
         aqua = [0, 1, 1],
         fuchsia = [1, 0, 1],
         red = [1, 0, 0],
         green = [0, 0.5, 0],
         blue = [0, 0, 1],
         purple = [0.5,  0, 0.5],
         maroon = [0.5, 0, 0],
         olive = [0.5, 0.5, 0],
         navy = [0, 0, 0.5],
         teal = [0, 0.5, 0.5]
    };
    
    To specify a color, one can give the rgb vector like
    fig.plot(x = xx, y = yy, color = [0.5, 0.7, 0.2];
    
    or use one of the predefined colors like
    fig.plot(x = xx, y = yy, color = colors.olive};
    

    New color definition can be easily added to the global variable colors. For example:

    global.colors.lavender = [0.6, 0.35, 0.73];
    

oz 2009-12-22