### 2-D Line Plot (plot)

Using plot command, you could plot **y = f(x)** type graph. Before we plot the graph, we need to enter data. So first of all, we need to initialize values for **x** variable.

Lets take -5, -4.5, -4, … , 4.5, 5 as variable **x** values. Since values of **x** are in an array from -5 to 5 with 0.5 increment between values, we could initialize **x** as follows.

**variableName = minValue:increment:maxValue;**

**Eg:** x = -5:0.5:5;

**Note:**

**variableName = [minValue:increment:maxValue];**

**y = x**. You could use following code to do that.

^{2}+ x + 1**x = -5:0.5:5;**

**y = x.^2 + x + 1;**

**Note:**

You cannot use **x^2** instead of **x.^2** here. Because here **x** is a matrix with only one row and many columns. To use **x^2**, that matrix must be a scalar and square matrix. So we have to compute element-wise power using **x.^2** here.

Now let’s get a new figure to plot the graph. (Even if you haven’t use this line. The graph will be plotted in a new figure.)

**x = -5:0.5:5;**

**y = x.^2 + x + 1;**

**figure;**

Then plot the graph using plot command using following format.

**plot(x,y);**

Now run the script and you will get following output.

In plot command, you could use following format to give line styles of the curve.

**plot(x, y, ‘lineSpecifications’);**

There are three types of line specifications that you could use in here.

- Line Style
- Marker Symbol
- Color

### Line Style Specifiers

**‘-‘**———- Solid line (This is the default style in MATLAB)**‘–‘**——— Dashed Line**‘:’**———- Dotted Line**‘-.’**——— Dash-dot Line

### Marker Specifications

**‘+’**———————– Plus sign**‘o’**———————– Circle**‘*’**———————– Asterisk**‘.’**————————Point**‘x’**———————– Cross**‘square’**or**‘s’**——– Sqare**‘diamond’**or**‘d’**—– Diamond**‘^’**———————– Upward pointing triangle**‘v’**———————– Downward pointing triangle**‘>’**———————– Right pointing triangle**‘<‘**———————– Left pointing triangle**‘pentagram’**or**‘p’**— Five-pointed Star (pentagon)**‘hexagram’**or**‘h’**—- Six-pointed Star (hexagram)

### Color Specifications

**‘r’**——— Red**‘g’**——— Green**‘b’**——— Blue**‘c’**——— Cyan**‘m’**——— Magenta**‘y’**——— Yellow**‘k’**——— Black**‘w’**——— White

You could use one or more specifications but only one from each one of here. As an example you cannot use both red color and green color in line specification. As an example if you use the following line specifications to the graph you have plotted before,

**plot(x,y, ‘-.sr’);**

You must get Dash-dot line (**-.**) with square markers (**s**) in red color (**r**).

#### Try yourself:

Since you have plenty of line styles, use them and try more line styles in your graphs.

Now let’s do a few more things in graphs. If you need to plot more than one graph in a figure, you need to hold on the figure. You can do it by using the following code.

**hold on;**

When you need to hold off, you could use following code.

**hold off;**

So let’s plot **y = 2x ^{2} **in the previous graph.

Now use following method to give a title to the graph and to name x-axis and y-axis.

**title(‘Two Curves’);**

**xlabel(‘x values’);**

**ylabel(‘y values’);**

Now you can see your title, x label and y label. Finally what you have to do is naming the two graphs. For that you could use following code.

**legend(‘Curve 01′,’Curve 02’);**

**Note:**

You can move graph names using your mouse to any place.

When using legend command, you need to name your graphs in the order that you have plotted them.

If you have more than two graphs, you can use legend command in the following way.

legend(‘Graph01′,’Graph02′,’Graph03’, … , ‘Graph0n’);

#### Try yourself:

Create some graphs and practice what you have learnt until now.

Source: http://www.mathworks.com

If you have ideas to improve this article, please comment below or email them to FOS Media.