We have moved to @ Placement Papers Hub !! Visit our new website here for more..

MATLAB Plotting as Multiple and Subplot


MATLAB Plotting as Multiple and Subplot

You can plot more than one function on the same figure. Let's say you want to plot a sine wave and cosine wave on the same set of axes, using a different color and point marker for each. The following m-file could be used to do this

» x = linspace(0,2*pi,50);               
» y = sin(x);
» z = cos(x);
» plot(x,y,'r', x,z,'gx')











More  than one plot can be put on the same figure using the subplot command. The subplot command allows you to separate the figure into as many plots as desired, and put them all in one figure. To use this command, the following line of code is entered into the Matlab command window or an m-file:

» subplot(m,n,p)

This command splits the figure into a matrix of m rows and n columns, thereby creating m*n plots on one figure. The p'th plot is selected as the currently active plot. For instance, suppose you want to see a sine wave, cosine wave, and tangent wave plotted on the same figure, but not on the same axis. The following m-file will accomplish this:

» x = linspace(0,2*pi,50);    

» y = sin(x);
» z = cos(x);
» w = tan(x);
» subplot(2,2,1)
» plot(x,y)
» subplot(2,2,2)
» plot(x,z)
» subplot(2,2,3)
» plot(x,w)















There are only three plots, even though I created a 2 x 2 
matrix of 4 subplots.Thus, you do not have to fill all of the subplots you have created, but Matlab will leave a spot for every position in the matrix. The subplots are arranged in the same manner as you would read a book. The first subplot is in the top left corner, the next is to its right.