2D Graphics

First we must generate the data then produce the "meaningful" 2D plot. MATLAB's fundamental data structure of the vector is useful. We consider our data to consist of (x,y) coordinates, where both x and y are vectors of a length choosen to generate enough data for a "smooth" plot.

x = linspace(0,2*pi,30);

Generates 30 equally spaced values between 2 and 2 pi. This assignment could also have been accomplished with the colon operator, though it would have taken a few steps:

dx = 2*pi/30;

What is the increment?

x = 0:dx:2*pi;

There is a slight difference though between these two ways to set up x. The "linspace" command will generate x(30) = 2*pi. The "colon" assignment will result in

x(30) = 0 + dx + dx + ... + dx

adding dx thirty times, which will be within floating point roundoff of being 2 pi.

The y coordinate is then generated by the array assignment

y = sin(x);

and the 2D plot by

plot(x,y)

As we explore these in the lab today, you should look at the effect of varying the value "30" above. MATLAB's online

help plot


 PLOT   Plot vectors or matrices. 
        PLOT(X,Y) plots vector X versus vector Y. If X or Y is a matrix,
        then the vector is plotted versus the rows or columns of the matrix,
        whichever line up. 
 
        PLOT(Y) plots the columns of Y versus their index.
        If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).
        In all other uses of PLOT, the imaginary part is ignored.
 
        Various line types, plot symbols and colors may be obtained with
        PLOT(X,Y,S) where S is a 1, 2 or 3 character string made from
        the following characters:
 
               y     yellow        .     point
               m     magenta       o     circle
               c     cyan          x     x-mark
               r     red           +     plus
               g     green         -     solid
               b     blue          *     star
               w     white         :     dotted
               k     black         -.    dashdot
                                   --    dashed
                              
        For example, PLOT(X,Y,'c+') plots a cyan plus at each data point.
 
        PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
        the (X,Y,S) triples, where the X's and Y's are vectors or matrices 
        and the S's are strings.  
 
        For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a
        solid yellow line interpolating green circles at the data points.
 
        The PLOT command, if no color is specified, makes automatic use of
        the colors specified by the axes ColorOrder property.  The default
        ColorOrder is listed in the table above for color systems where the
        default is yellow for one line, and for multiple lines, to cycle
        through the first six colors in the table.  For monochrome systems,
        PLOT cycles over the axes LineStyleOrder property.
 
        PLOT returns a column vector of handles to LINE objects, one
        handle per line. 
 
        The X,Y pairs, or X,Y,S triples, can be followed by 
        parameter/value pairs to specify additional properties 
        of the lines.
                                        
        See also SEMILOGX, SEMILOGY, LOGLOG, GRID, CLF, CLC, TITLE,
        XLABEL, YLABEL, AXIS, AXES, HOLD, and SUBPLOT.

To make graphics presentations usable, they need some labelling to keep their context clear. Titles, x- and y-axis labels so we know the units and other text annotations help a graph "speak" to the viewer. When you first construct a plot, you typically are so familiar with what the data represents, that this seems necessary. But a week later, you may not recall, for example, the "units" on the x-axis for the data. So it's a good practice to place as much labelling as possible on the plots you produce.

help title

help xlabel

help ylabel

help text

 TITLE  Titles for 2-D and 3-D plots.
        TITLE('text') adds text at the top of the current axis.
 
        TITLE('text','Property1',PropertyValue1,'Property2',PropertyValue2,...)
        sets the values of the specified properties of the title.
 
        See also XLABEL, YLABEL, ZLABEL, TEXT.

>> help xlabel

 XLABEL X-axis labels for 2-D and 3-D plots.
        XLABEL('text') adds text beside the X-axis on the current axis.
 
        XLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...)
        sets the values of the specified properties of the xlabel.
 
        See also YLABEL, ZLABEL, TITLE, TEXT.

>> help ylabel

 YLABEL Y-axis labels for 2-D and 3-D plots.
        YLABEL('text') adds text beside the Y-axis on the current axis.
 
        YLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...)
        sets the values of the specified properties of the ylabel.
 
        See also XLABEL, ZLABEL, TITLE, TEXT.

>> help text

 TEXT   Add Text to the current plot.
        TEXT(X,Y,'string') adds the text in the quotes to location (X,Y)
        on the current axes, where (X,Y) is in units from the current
        plot. If X and Y are vectors, TEXT writes the text at all locations
        given. If 'string' is an array the same number of rows as the
        length of X and Y, TEXT marks each point with the corresponding row
        of the 'string' array.
 
        TEXT(X,Y,Z,'string') adds text in 3-D coordinates.
 
        TEXT returns a column vector of handles to TEXT objects, one
        handle per text object. TEXT objects are children of AXES objects.
 
        The X,Y pair (X,Y,Z triple for 3-D) can be followed by 
        parameter/value pairs to specify additional properties of the text.
        The X,Y pair (X,Y,Z triple for 3-D) can be omitted entirely, and
        all properties specified using parameter/value pairs.
 
        Execute GET(H), where H is a text handle, to see a list of text
        object properties and their current values. Execute SET(H) to see a
        list of text object properties and legal property values.
 
        See also XLABEL, YLABEL, ZLABEL, TITLE, GTEXT, LINE, PATCH.

On to our Text's Section 2.5 example