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.
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.
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′)
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.
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?
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
%% Put a and b on one plot
% Make two plots
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.
%% Method 2. multiple lines with ‘hold on’ command
%% See the effect of hold on and hold off
%% See the effect of hold on and figure
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];
y = sin(x-z(i));
3. Using the line Command
It’s a low level graphics command, adds additional graphs(lines) to a plot that already exits.
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
line(x,y2,’linestyle’, ‘–‘, ‘color’, ‘r’, ‘marker’, ‘o’);
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)