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
plot(y);

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

clip_image001

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
plot(y);

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

plot(x,y);

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

  • Line pattern
  • Line colour
  • Line width
  • Marker (colour, size, etc.)
x=0:10:100;
y = sin(0.01*pi*x);
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);

This results as follows.

clip_image002

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);
plot(y1);
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
plot(y2);

clip_image003

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);
set(gca,'XTick',-pi:pi/2:pi);
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'});
xlabel('-\pi \leq \Theta \leq \pi');
ylabel('sin(\Theta)');
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.

clip_image004

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;
plot(x,cos(x),'-ro',x,sin(x),'-.b');
hleg1 = legend('cos_x','sin_x');

Which will results as

clip_image005

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 | , , ,

4 Comments »

  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

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: