**Plotting with
MATLAB**

** **

We have been exposed to different MATLAB commands that generate and customize plots of data. To a great extent this has been accomplished in a trial-and-error fashion (or a use and abuse fashion) and it is appropriate now to make some further statements about these plotting capabilities.

**xy plots**

The basic plot command in MATLAB generates as an output, an xy plot of data provided as inputs or arguments to the plot function. The data here is typically organized in arrays and depending on the nature and size of this array data, the plot command interprets the data in various ways.

- plot(A) plots the columns of A verses their indices and generates n curves where matrix A is a matrix with m rows and n columns. ie.. it plots each column of A versus an integer variable (or index variable), as a separate curve on the plot.
- plot(x,A) plots the matrix A versus the vector x, where x is either a row vector or a column vector and A is a matrix with m rows and n columns. If the length of x is m, then each column of A is plotted versus the vector x. If x has a length of n, then each row of A is plotted versus the vector x. If m=n then the columns of A are plotted vs. x.

**Data markers and line types**

** **

When multiple curves are plotted on a figure in MATLAB, different colors are automatically selected. If different colors, line styles or data markers are preferred these can be specified as input arguments to the plot command. Table 4.2-1 details the different colors, line styles and data markers that are available. These can also be modified in the figure window in MATLAB.

**Labeling curves and data**

Several commands are provided to allow you to customize and label plots in MATLAB. We have used most of these in class but you should feel free to experiment with the use of the following:

- title('…….') - this command adds a title caption to a plot
- xlabel('….') - this command adds an x-axis label to a plot
- ylabel('….') - this command adds a y-axis label to a plot
- grid - this command adds a rectangular grid to a plot
- legend('...') - this commands adds a moveable legend to the plot
- axes([xmin xmax ymin ymax]) - this command allows you to change the x-axis and y-axis limits on a plot

**Other xy plots**

MATLAB provides a variety of other plot formats for you to use. Most of these are commonly used in different situations.

- subplot - lets you subdivide a figure into several plots that can be constructed separately
- semilogx - lets you plot data with a logarithmic x-axis
- loglog - lets you plot data with logarithmic x- and y-axes
- stem - plots data as vertical lines in an xy plot
- stairs - plots data as a staircase function
- bar - plots data as a bar graph
- polar - plots data in polar coordinate form (q,r)

**3-D plots**

MATLAB has several plot commands that generate three-dimensional plots of data. There are basically four different types of plots that can be constructed with some variations.

- plot3(x,y,z) - plots a 3-D curve from data arrays x, y and z. A typical use is when the coordinates of an object (x,y,z) change as a function of another variable, typically time. Here x, y and z are usually expressed as parametric equations.
- contour(x,y,z) - generates a contour plot of the function z, that varies with x and y. Used commonly for topographic data.
- mesh(x,y,z) (meshz, meshc) - plots a surface plot of a function z, that varies with x and y.
- surf(x,y,z) (surfc) - plots a surface plot of the function z , that varies with x and y.
- [X,Y]=meshgrid(….,….) - generates matrices (arrays) for the input variables X and Y used to calculate a surface plot of a function, z.

**Example:**

** **

>>
[d,th]=meshgrid([1:.1:10],[.4:.01:1.4]);

>>
L=(100./d)-(d./tan(th))+2*(d./sin(th));

>> mesh(d,th,L)

The plot can now be customized with the usual labels, grids, etc..