**The **`plot`

Statement

`plot`

StatementProbably the most common method for creating a plot is by issuing `plot(x, y)`

statement where function y is plotted against x.

#### Example 1

Type in the following statement at the MATLAB prompt:

`x=[-pi:.1:pi]; y=sin(x); plot(x,y);`

After we executed the statement above, a plot named Figure1 is generated:

Having variables assigned in the Workspace, x and y=sin(x) in our case, we can also select x and y, and right click on the selected variables. This opens a menu from which we choose plot(x,y). See the figure below.

**Annotating Plots**

Graphs without labels are incomplete and labeling elements such as plot title, labels for x and y axes, and legend should be included. Using up arrow, recall the statement above and add the annotation commands as shown below.

`x=[-pi:.1:pi];y=sin(x);plot(x,y);title('Graph of y=sin(x)');xlabel('x');ylabel('sin(x)');grid on`

Run the file and compare your result with the first one.

#### Aside:

```
help gtext
help legend
help zlabel
```

**Superimposed Plots**

If you want to merge data from two graphs, rather than create a new graph from scratch, you can superimpose the two using a simple trick:

```
% This script generates sin(x) and cos(x) plot on the same graph
% initialize variables
x=[-pi:.1:pi]; %create a row vector from -pi to +pi with .1 increments
y0=sin(x); %calculate sine value for each x
y1=cos(x); %calculate cosine value for each x
% Plot sin(x) and cos(x) on the same graph
plot(x,y0,x,y1);
title('Graph of sin(x) and cos(x)'); %Title of graph
xlabel('x'); %Label of x axis
ylabel('sin(x), cos(x)'); %Label of y axis
legend('sin(x)','cos(x)'); %Insert legend in the same order as y0 and y1 calculated
grid on %Graph grid is turned
```

**Multiple Plots in a Figure**

Multiple plots in a single figure can be generated with `subplot`

in the Command Window. However, this time we will use the built-in Plot Tools. Before we initialize that tool set, let us create the necessary variables using the following script:

```
% This script generates sin(x) and cos(x) variables
clc %Clears command window
clear all %Clears the variable space
close all %Closes all figures
X1=[-2*pi:.1:2*pi]; %Creates a row vector from -2*pi to 2*pi with .1 increments
Y1=sin(X1); %Calculates sine value for each x
Y2=cos(X1); %Calculates cosine value for each x
Y3=Y1+Y2; %Calculates sin(x)+cos(x)
Y4=Y1-Y2; %Calculates sin(x)-cos(x)
```

Note that the above script clears the command window and variable workspace. It also closes any open Figures. After running the script, we will have X1, Y1, Y2, Y3 and Y4 loaded in the workspace. Next, select File > New > Figure, a new Figure window will open. Click "Show Plot Tools and Dock Figure" on the tool bar.

Under New Subplots > 2D Axes, select four vertical boxes that will create four subplots in one figure. Also notice, the five variables we created earlier are listed under Variables.

After the subplots have been created, select the first supblot and click on "Add Data". In the dialog box, set X Data Source to X1 and Y Data Source to Y1. Repeat this step for the remaining subplots paying attention to Y Data Source (Y2, Y3 and Y4 need to be selected in the subsequent steps while X1 is always the X Data Source).

Next, select the first item in "Plot Browser" and activate the "Property Editor". Fill out the fields as shown in the figure below. Repeat this step for all subplots.

Save the figure as `sinxcosx.fig`

in the current directory.