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)

  1. Introduction to basics and layout 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 ‘pi/100’ and ends at ‘2*pi’. 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.

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.

Defining time axis limits and Functions to be plotted (MATLAB Plot)
Figure 1: Defining time axis limits and Functions to be plotted

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:

Plot of COS function in MATLAB using plot command
Figure 2: Use of MATLAB plot command for plotting a function

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 “ylabel(‘text’)” . the text is written as a string in single quotes.


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:

Labeling axes and title of plot
Figure 3: Labeling axes and title of plot


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 “subplot (2,2,1);” 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); 
plot (t,x);
xlabel (‘t’);
ylabel (‘cos(t));

our graph will look like figure 3.

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

Subplot command in MATLAB
Figure 4: Subplot command in MATLAB

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)’); 

our graph will look like:

 Stem plot in subplot in MATLAB
Figure 5: Stem plot in subplot in MATLAB


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.


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

plot3 (x,y,z);
axis( [-11 -11 -11] );

our graph will look like:

plot3 command for plotting 3d graphs in MATLAB
Figure 6: plot3 command in MATLAB

Multiple Plots on Single Plot:


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:


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.


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

plot (t,x, ’r’, ‘linewidth’,2);
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:

 Mulitple Plot on single graph in MATLAB
Figure 8: Mulitple Plot on single graph in MATLAB


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.

Asad Ullah

I am MSc Electrical Scholar under a fellowship program. Working with Modeling and Simulation software related to my field are my activities in leisure.

What do you think?