We use multiple plots to show more than one set of data on the same graph. This helps us compare data easily.
Multiple plots (hold on) in 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.
x = 1:5; y1 = x; y2 = x.^2; plot(x, y1) hold on plot(x, y2) hold off
t = 0:0.1:2*pi; y1 = sin(t); y2 = cos(t); plot(t, y1, '-r') hold on plot(t, y2, '--b') hold off
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.
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
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.
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.
Practice
hold on command do in MATLAB plotting?Solution
Step 1: Understand the purpose of
Thehold onhold oncommand tells MATLAB to keep the current plot and add new plots on top of it.Step 2: Compare with other options
Other commands likehold offor default plotting clear the figure before new plots, so they do not allow multiple plots together.Final Answer:
Allows multiple plots to be drawn on the same figure without erasing previous plots. -> Option BQuick Check:
hold on= multiple plots on one figure [OK]
- Thinking hold on clears the figure
- Confusing hold on with saving plots
- Assuming hold on changes plot colors
Solution
Step 1: Identify the correct sequence to keep multiple plots
Usinghold onafter the first plot keeps it visible for the second plot.Step 2: Confirm proper use of hold off
Callinghold offafter plotting both lines resets plotting behavior for future plots.Final Answer:
plot(x1, y1); hold on; plot(x2, y2); hold off; -> Option AQuick Check:
Use hold on between plots to combine them [OK]
- Not using hold on before second plot
- Using hold off too early
- Plotting without hold on clears previous plot
t = 0:0.1:1;
plot(t, t);
hold on;
plot(t, t.^2);
Solution
Step 1: Analyze the first plot command
The first plot draws a straight line y = t on the figure.Step 2: Understand the effect of hold on and second plot
Usinghold onkeeps the first plot visible, so the second plot y = t^2 is drawn on the same figure as a curve.Final Answer:
Two plots will appear: a straight line and a curve on the same figure. -> Option AQuick Check:
hold on keeps previous plot, so both appear [OK]
- Thinking only last plot shows
- Assuming hold on causes error
- Confusing plot commands order
plot(x, y1)
hold off
plot(x, y2)
Solution
Step 1: Understand the effect of hold off
Callinghold offresets plotting to clear the figure before the next plot.Step 2: Identify correct command to keep plots
To keep both plots visible,hold onshould be used instead ofhold off.Final Answer:
hold off should be replaced with hold on to keep both plots visible. -> Option DQuick Check:
Use hold on to combine plots, not hold off [OK]
- Using hold off instead of hold on
- Thinking hold off adds plots
- Ignoring hold commands altogether
Solution
Step 1: Use hold on after first plot to combine multiple plots
After plotting the first data set,hold onkeeps the figure active for additional plots.Step 2: Plot all data sets with specified colors and then reset with hold off
Plotting the second and third data sets adds them to the same figure. Callinghold offresets plotting behavior for future plots.Final Answer:
plot(x1, y1, 'r'); hold on; plot(x2, y2, 'g'); plot(x3, y3, 'b'); hold off; -> Option CQuick Check:
hold on after first plot, hold off after last plot [OK]
- Not calling hold off after multiple plots
- Calling hold on before first plot
- Mixing hold on and hold off incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
