# Basics of Plotting Graphs in MATLAB

In this article we will see that how commands like ** plot, stem, subplot, xlabel, ylabel, title, plot, stem, subplot, axis, plot3, colour, linewidth, hold and legend ** are used in Plotting graphs in MATLAB.

*( if you are new to MATLAB, first check this article to learn basics of MATLAB)*

First we open the command window and type *‘clear all’* so that our workspace is cleared.

## 1. Defining x-axis and Functions:

We now define our first variable ‘t’,

t= -2*pi : pi/100: 2*pi;

(where pi=3.1416)

This gives us an AP (arithmetic progression) in form of column matrix whose first value is ** ‘-2*pi’** , increments with a value of

**and ends at**

*‘pi/100’***. See here we have used ‘;’after our command so that our value for that command gets stored in workspace otherwise it would have appeared below our command and it would have taken more space in our command window.**

*‘2*pi’*Our second variable is ** x = cos(t)**; which gives us cosine values of variable ‘t’. here the values are in radian. If we want our values in degrees then we should have defined our variables like:-

t = [ 0 : 10 : 360]; x = cosd(t);

the *‘d’* defines degrees. It should be noted that ** ‘cosd’** is a complete command for degrees value of function.

Similarly y = sin(t); gives sine values of variable ‘t’.

Our next variable

z = x.^2 + y.^2;

which is an equation of circle.

## 2. PLOT:

Our next command is * plot (t , x);* which makes a continuous time graph whose x-axis contains values of variable ‘t’ and y-axis contains values of variable ‘x’. In general, to plot any graph we use the command “plot (x_function, y_function)”. After this command we will get a cosine graph as shown in figure:

## 3. Labeling the axes:

### Xlabel and Ylabel:

To easily understand this graph we can label it. To label x-axis the command used is “** xlabel (‘text’)**”. Similarly for y-axis the command used is “

**” . the text is written as a string in single quotes.**

*ylabel(‘text’)*### Title:

The command used for title is “** title (‘text’)**”. So after using the commands

xlabel (‘t’), ylabel (‘cos(t)’) and title (‘PLOT OF COS FUNCTION’) ,our graph will look like:

## SUBPLOT:

If we want to plot more than one graph in a single window we can do it so by using subplot. This command is always used before ‘plot’ command. The command is “** subplot (m,n,p)**;” .This divides the figure into a matrix of ‘m’ rows and ‘n’ columns, ‘p’ specifies the graph number that is to be handled. The command “

**” will divide our figure in two rows and two columns i.e, in total 4 parts. ‘1’ describes that we are plotting in first part of figure. So after using the commands:**

*subplot (2,2,1);*subplot (2,2,1); plot (t,x); xlabel (‘t’); ylabel (‘cos(t)); title (‘PLOT OF COS FUNCTION’)

our graph will look like figure 3.

similarly after using the commands subplot(2,2,2); plot (t,y); the graph will look like

## Stem Plot:

The stem command is used to make discrete time graph of the data. The property of stem command is same as that of plot command. To use stem command type “** stem (x_function, y_function);**”. And after using the commands:

subplot (2,2,2); stem (t,y); xlabel (‘t’); ylabel (‘sin(t)’); title (‘PLOT OF SIN FUNCTION’);

our graph will look like:

## AXIS:

The axis command is used to set the desired axis limits of the graph. The command used is “axis ( [range of x-axis_range of y-axis_range of z-axis] )”. The range of z-axis is required in plotting 3D graph only. So the command “axis( [-0.5 0.5 -1.5 1.5] )” used here is to show the axis range.

## PLOT3:

This command is used to make 3D graphs. For this three variables for x, y and z axis are used. The command used is “plot3 (x-axis, y-axis, z-axis)”. So after the commands

Subplot(2,2,3);

plot3 (x,y,z);

axis( [-11 -11 -11] );

our graph will look like:

## Multiple Plots on Single Plot:

### HOLD:

The hold command is used to plot multiple functions on the same graph. After the command ‘plot’ just type hold and then we can draw another graph on that same graph by changing plot variables as shown in figure 7:

### COLOUR and LINEWIDTH:

To differentiate multiple graphs the colour and linewidth commands are used. The command used is plot (x-variable, y-variable, ‘r’,’linewidth’,2); the ‘r’ means the colour red. To use any other colour type the initial of that colour. Example ‘b’ for blue, ‘g’ for green etc. it should be noted that the initial of that colour name should be within single inverted commas after the x and y variable. The linewidth command will give the width of line. It may be used directly after x and y variable. It should also be enclosed within single inverted commas. The number ‘5’ here defines the width of the line and is written after separating by a comma as shown.

### LEGEND

The legend command is used to specify legends to each plotted function on the graph. The command is ** “legend(‘first graph name’, ‘second graph name’, ‘third graph name’)**; so after these commands

subplot(2,2,4); plot (t,x, ’r’, ‘linewidth’,2); hold plot (t,y, ’g’, ‘linewidth’,2); plot (t,z, ’y’, ‘linewidth’,4); legend( ‘cos (t)’, ‘sin(t)’, ‘unit circle’);

our graph will look like as in figure 8:

So we saw different commands that are used in MATLAB. It should be kept in mind that there is a proper format for each command. We have to remember the syntax for each command. These commands will surely help you in your projects.

More articles which can help you building your project using MATLAB.

For feedback, comment below or contact us for any further details and guidance. Like out facebook page for more.