We use multiple plots to show more than one set of data on the same graph. This helps us compare data easily.
0
0
Multiple plots (hold on) in MATLAB
Introduction
Comparing sales data from two different years on one graph.
Showing temperature changes for two cities on the same plot.
Plotting actual data and predicted data together to see how close they are.
Displaying multiple sensor readings over time on one chart.
Syntax
MATLAB
plot(x1, y1) hold on plot(x2, y2) hold off
hold on tells MATLAB to keep the current plot so you can add more plots on top.
hold off stops adding to the current plot and resets for new plots.
Examples
Plots a line and a curve on the same graph.
MATLAB
x = 1:5; y1 = x; y2 = x.^2; plot(x, y1) hold on plot(x, y2) hold off
Plots sine and cosine waves with different colors and line styles.
MATLAB
t = 0:0.1:2*pi; y1 = sin(t); y2 = cos(t); plot(t, y1, '-r') hold on plot(t, y2, '--b') hold off
Sample Program
This program plots a linear and a quadratic function on the same graph. It uses markers to show points and adds a title, labels, and legend for clarity.
MATLAB
x = 0:0.5:5; y1 = x; y2 = x.^2; plot(x, y1, '-o') hold on plot(x, y2, '-x') title('Multiple plots with hold on') xlabel('X axis') ylabel('Y axis') legend('Linear', 'Quadratic') hold off
OutputSuccess
Important Notes
Always use hold off after finishing multiple plots to reset the plot state.
You can plot as many lines as you want between hold on and hold off.
Use different colors or markers to make each plot easy to see.
Summary
Use hold on to add multiple plots on the same figure.
Remember to use hold off to stop adding plots.
Multiple plots help compare data clearly in one graph.