Thilina's Blog

I might be wrong, but…

MATLAB plotting tools 1.0 – ‘plot’ command

Data visualization is a main requirement in Image and Audio signal processing, since it can be used to identify special features, intermediate results, process states as well as the correctness of the final results. Data plotting is one way of data visualization which used in very common by all MATLAB users. However the selection and handling of plot for the requirement is a critical factor since the whole representation of the data is depends on the tool we choose to visualize it. In this article (or maybe a set of articles) I am going to share my experience and suggestions on using MATLAB plotting tools.

plot Command

The command ‘plot’ is the most widely used data visualization tool which will create a 2D line plot on a MATLAB figure. Say you are having a data set called ‘y’ based on values ‘x’, you can simply visualize them using the plot command as follows.

x = 0:0.001:100; % x is the array of 0 to 100 with 0.001 intervals
y = sin(0.05*pi*x); % y is the sine value array of x

After this command plot will be drawn in a MATLAB figure as follows;


However if there exist a previous plot this will be drawn on top of it. Therefore the best practice is to initiate a figure before plotting some data.

h = figure; % h is a MATLAB figure

In some cases its better to add your x values to the plot as well. Now the plot command will be as follows;


We can add some special features for the plot as follows such as

  • Line pattern
  • Line colour
  • Line width
  • Marker (colour, size, etc.)
y = sin(0.01*pi*x);

This results as follows.


Next let’s see how to plot two different graphs in same plot. For this we are using hold on, hold all and hold off commands as follows.

x= 0:0.01:200;
y1 = sin(0.01*pi*x);
y2 = sin((0.01*pi*x)+(pi/4));
% hold axes and all lineseries properties, such as
% ColorOrder and LineStyleOrder, for the next plot
hold all


Let’s have a look how to add text labels and annotations to the a MATLAB plot.

x = -pi:.1:pi;
y = sin(x);
p = plot(x,y);
xlabel('-\pi \leq \Theta \leq \pi');
title('Plot of sin(\Theta)');
% \Theta is Greek symbol Θ
% Annotate the point (-pi/4, sin(-pi/4))
text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi\div4)',...
'HorizontalAlignment','left'); %text(x,y,’text’);
% Change the line color to red and set the line width to 2 points
set(p,'Color','red','LineWidth',2); %set(item,’parameter’, value,’parameter’, value, …..);

The command gca means ‘get current axis’ and the changes we do applies to this current (active) plot.


We can limit the range of viewing by using xlim and ylim commands as below;

xlim([-pi pi]); %[lower_limit upper_limit]);
ylim([-2 2]);

Finally this is the way how to put legend to a plot.

x = -pi:pi/20:pi;
hleg1 = legend('cos_x','sin_x');

Which will results as


I hope you got a brief idea on how to use MATLAB plot. For further details you can use MATLAB documentation by typing

doc plot

in MATLAB command window. Most of the examples used in here are taken from MATLAB documentation. In my future articles I will share how to use other plotting tools such as stem, mesh and surf. Thank you very much for reading.

2013 February 1 - Posted by | MATLAB | , , ,


  1. Nice to see u back ayyaa..

    Comment by Lakshitha | 2013 February 1 | Reply

    • Thank you so much malli 🙂

      Comment by Thilina S. | 2013 February 1 | Reply

  2. can you explain how to plot a polynomial equation 🙂

    Comment by ratmcu | 2013 February 14 | Reply

    • hope you got the answer malli 🙂

      Comment by Thilina S. | 2013 February 18 | Reply

Leave a Reply to ratmcu Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: