# Multiple Graphs Or Plot Overlays in same MATLAB Plot

In this sections explains with Matlab examples and with video on Multiple Graphs Or Plot Overlays in same MATLAB Plot (Formatting of MATLAB Plots)

## 1. Using the plot command

If “the complete data sets are available”, like (x1, y1), (x2, y2), and (x3, y3). Then two or more graphs can be created in the same plot.

The form is plot(x1, y1, x2, y2, x3, y3), which creates three graphs: x1 vs y1, x2 vs y2 and x3 vs y3, all in the same plot.

With Line specifiers:

plot(x1,y1, ‘r:’,x2,y2, ‘k-.’,x3,y3, ‘m—’)

Note: All the vectors of each pair (xi, Yi) must be of the same length.

Examples

1. Plot the following functions, $$y_1=sin(x)$$,    $$y_2=sin(x-0.35)$$,    $$y_3=sin(x-0.55)$$, and    $$y_4=sin(x-0.85)$$    For $$o\leqslant x\leqslant 2 \pi$$ using matlab plot command.

Matlab Program

x = 0:pi/100:2*pi;

y1 = sin(x);

y2 = sin(x – .35);

y3 = sin(x – .55);

y4 = sin(x – .85);

plot(x, y1,’r’, x, y2,’b:’, x, y3,’g–‘,x,y4,’k-.’)

legend(‘sin(x)’, ‘sin(x – .15)’, ‘sin(x – .25)’,’sin(x-.35′)

axis tight

2.Plot the graph for  $$x_1=2t^3-8t+10$$, $$x_2=6t^2-8$$, $$x_3=12t$$ and  $$x_4=12$$,  $$-3\leqslant t\leqslant 6$$, using plot command.

Matlab Program

t=-3:0.1:6;

x1=2*t.^3-8*t+10;

x2=6*t.^2-8;

x3=12*t;

x4=12;

plot(t,x1, ‘ro’,t,x2,’b–‘,t,x3,’k*:’,t,x4,’g*:’)

legend(‘x_1′,’x_2′,’x_3′,’x_4’)

axis tight

title(‘t vsx_1,x_2,x_3,x_4’); xlabel(‘t’);ylabel(‘x_1,x_2,x_3, x_4’)

## 2. Using Hold on and Hold off Command

Check plot (a) and then plot (b)

What do you see?

Only b

Note: Matlab will replace the current plot with any new one unless you specifically tell it not to

To have both plots on one figure use the hold on command

plot(x,y1,’k’)

pause(3)

plot(x,y2,’r’)

%% Put a and b on one plot

plot(x,y1,’k’)

hold on;

pause(5)

plot(x,y2,’r’)

% Make two plots

plot(x,y1)

figure;

plot(x,y2)

## Using the hold on, hold off Commands

Hold on command only needs to be issued once per figure, thus calling hold on will result in all subsequent plot commands going to one figure unless a new figure command is issued or hold off is used.

Try following

%% Method 2. multiple lines with ‘hold on’ command

%% See the effect of hold on and hold off

plot(x,y1);

hold on;

pause(2)

plot(x,y2,’r’);

pause(2)

plot(x,y3,’g’);

hold off;

pause(2)

plot(x,y4,’m’);

%% See the effect of hold on and figure

plot(x,y1);

hold on;

pause(2)

plot(x,y2,’r’);

pause(2)

plot(x,y3,’g’);

pause(2)

hold off;

pause(2)

figure

plot(x,y4,’m’);

%%

plot(x,y1);

hold on;

pause(2)

plot(x,y2,’r’);

pause(2)

plot(x,y3,’g’);

pause(2)

figure

plot(x,y4,’m’);

pause(2)

plot(x,y3,’g’);

Note: Using the hold on, hold off Commands

Here one graph is plotted first with the plot command and then the hold on command is typed which keeps the figure window open to add additional graphs.

Each plot command creates a graph that is added to the figure.

The hold off command stops this process.

Hold on is useful when completed data to be plotted is not available at the same time. This is used, as the data is available for the plotting. Ex: For loop.

%% For loop exmaple

x = 0:pi/100:2*pi;

z=[0 0.35 0.55 0.85];

for i=1:4;

y = sin(x-z(i));

plot(x,y)

pause(2)

hold on

end

hold off

axis tight

## 3. Using the line Command

The form of the line command is:

line(x data, y data, ‘PropertyName’, PropertyValue)

Ex: line( x, y, ‘linestyle’, ‘- -’, ‘color’, ‘r’, ‘marker’, ‘o’)

Note: The major difference between the plot and the line commands is that the plot command starts a new plot every time it is executed, while the line command adds lines to a plot that already exits.

%%3. Multiple lines with ‘line’ command

plot(x,y1);

%hold on;

pause(2)

line(x,y2,’linestyle’, ‘–‘, ‘color’, ‘r’, ‘marker’, ‘o’);

pause(3)

line(x,y3,’linestyle’, ‘–‘, ‘color’, ‘g’, ‘marker’, ‘*’);

## 4. Using Editor window

If you have two figure windows,

Just click on the arrow button of one plot, to enable plot editor mode and select the line to be copied and copy it.

Go to the plot other plot, where you want to keep or paste, again, select arrow in that figure window and paste the link you copied.

You can also open a new figure window and copy the line in to new one from the respective figures.(See above video for more details)