 Lesson 5

# Plotting and Graphing

1

Creating plots of data sets and functions is very useful for engineers and scientists. You can use plots to present results for papers and presentations or to visually search for approximate solutions to problems.

MATLAB has a rich set of plotting commands. In this chapter, we describe MATLAB’s basic plotting operations. We will consider the following areas

1. The Plot command

2. Using the plot editor

3. Line and Marker styles

4. Labeling a plot

5. Multiple plots

6. Scaling plots

## The "plot" command

plot() is the basic plotting command in MATLAB. When a vector is given to the plot() function, MATLAB plots the elements in the vector against the indices of the vector. Suppose we are supposed to graph a sine function for angle 0 to 360 degrees. (Note: the angles ought to be in radians ). The following programming commands in MATLAB written.

When called the command plot(sin(theta)) automatically opens the Plot Editor window and creates the plot. The output will be a graph of the vector of sine of the angles against the indices of the vector as shown below.

If however, two vectors, X and Y, are passed to the plot command, MATLAB plots the corresponding pairs in the X and Y vectors and draws a single line connecting the points. The following example is a plot of two vectors sin(theta) and theta indeed, the same plot as that in the previous example.

It is possible to plot a matrix against a vector using the plot function, as long as the row vector length is equal to the length of all the rows in the matrix as shown. Since the columns of X and Y are of the same order, but the rows are not, the plot function creates two lines, one for each row. The first line is a plot of row one of Y against X. The second line is a plot of row two of Y against X

## Using the plot editor

To use the plot editor, go to the Menu bar of Plot editor, Choose Insert from the Menu bar of Plot Editor, and a drop-down menu will appear. The figure below shows the Insert drop-down menu of the Plot Editor. From this menu, you can insert and modify many plot elements.

From the drop-down list we can for example label the axis and put a legend for out data.

## Line and Marker styles

Color and texture can be added the plot lines or change using the command line options. These include the line specifier and other line properties. An addition argument can be added to the plot() function to control the color and texture. This argument is known as the line specifier as discussed in chapter two.

The line specifier can take three type, the line character, the marker symbol, and the color specifiers.

The line specifier could be :

The marker symbol is one of 13, and the marker codes are as follows:

The following are the color codes:

Now we will create a plot of the sine function with a diamond-shaped marker, a dashed line, and the color red. The symbols for the dashed line, diamond marker shape, and red color are enclosed in quotes as an argument to the plot command. The arguments within the quotes can be placed in any order

Other line qualitiescan be controlled by using property-value pairs as additional arguments to plot. Examples of line attributes are LineWidth and MarkerSize.

Let us re-create the previous plot, but use a solid line with and a circular marker with In the following command, the lowercase character o designates a circular marker:

## Labelling a plot

Plot labeling includes a title, axis labels, and if necessary, a legend. Labels, titles, legends, and other text can be created with the xlabel, ylabel, legend, title, and text commands.

The xlabel command has several syntactic variants. The first variant takes a string argument and displays it as the X-axis label:xlabel('string')

The second form takes a function as an argument:xlabel(function) The function must return a string. The ylabel is similar to the xlabel. For example: from previous example

The legend command creates a legend for the plot. You can pass the legend command either a list of strings that describe the legend’s contents or a matrix of strings. In the second case, each row of the string matrix becomes a line in the legend.

The syntax for the legend command is legend('str1','str2',...) or legend(string_matrix) . You can optionally supply an additional argument to legend that indicates the position of the legend in the graph: legend('str1','str2',...,position)

position of the legend command

For example

## Multiple plots

You can display multiple, simultaneous overlapping plots by using the hold command. The hold command is an example of a toggle. A toggle has two values: on or off, hold on cause the new graph to be superimposed on the existing graph while hold off refreshes the plot window.

You can display multiple non-overlapping plots by using the subplot command, which divides the Plot window into sub-windows called panes. The syntax is sublot(m,n,pane_number) For example, the command &gt;&gt; subplot(2,1,2) results in the Plot window being divided into 2 rows by 1 column of panes. The pane_number argument 2 indicates that the next plot command will place the subplot in pane number 2.

Let us plot the cosine and sign graph.

## Scalling

By default, the axes in MATLAB plots are linear. To plot a function with a logarithmic scale on the X-axis, use the semilogx command. Similarly, the semilogy command creates a logarithmic (or log) scale on the Y-axis. To create log scales on both axes, use the loglog command. These three commands have the same syntax and arguments as the plot command.

You may want to superimpose a grid over the graph when using semilog and log–log plots. Such a grid visually emphasizes the nonlinear scaling. Use the grid command to toggle a grid over the graph.

## Exercise

Write a script that performs the following tasks:

1. Create a vector X containing the values from to 10 in increments of 0.5.

2. Create a vector Y that contains the square of each value in X.

3. Plot X and Y, using a dashed line and a pentagram marker.

4. Create a title in italics that reads

5. Create appropriate labels for the X- and Y-axes