0
0
MATLABdata~10 mins

Multiple plots (hold on) in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple plots (hold on)
Start
Plot first graph
hold on activated
Plot second graph
Plot more graphs if needed
hold off to stop adding
End
The flow shows plotting the first graph, turning 'hold on' to keep the plot, adding more plots, then turning 'hold off' to finish.
Execution Sample
MATLAB
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1)
hold on
plot(x,y2)
hold off
This code plots sine and cosine on the same graph using 'hold on' to keep the first plot visible.
Execution Table
StepActionPlot StateOutput
1Create x, y1, y2 arraysNo plot yetVariables ready
2plot(x,y1)Plot shows sine curveSine curve visible
3hold onHold activatedNext plots added on same graph
4plot(x,y2)Sine and cosine curves visibleCosine curve added
5hold offHold deactivatedNo more plots added automatically
6EndFinal plot with sine and cosinePlot window shows both curves
💡 All plots added; hold off stops further additions.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
xundefined[0 0.1 ... 6.28][0 0.1 ... 6.28][0 0.1 ... 6.28][0 0.1 ... 6.28]
y1undefinedsin(x) valuessin(x) valuessin(x) valuessin(x) values
y2undefinedcos(x) valuescos(x) valuescos(x) valuescos(x) values
Key Moments - 2 Insights
Why does the second plot replace the first if 'hold on' is not used?
Without 'hold on', each plot command clears the previous plot, so only the last plot shows. See step 2 and 4 in execution_table where hold on is activated to keep both.
What happens if 'hold off' is not called after plotting multiple graphs?
'hold off' stops adding new plots on the same figure. If omitted, new plots will continue to add until MATLAB session ends or figure is closed. See step 5 for hold off effect.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the plot state after step 3?
AOnly sine curve is visible; hold is off
BHold is activated; next plots add on same graph
CPlot window is cleared
DCosine curve is visible alone
💡 Hint
Check the 'Plot State' column at step 3 in execution_table
At which step does the cosine curve first appear on the plot?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in execution_table for when cosine curve is added
If 'hold off' was not called, what would happen when plotting a third graph?
AThe third graph would replace the previous two
BAn error would occur
CThe third graph would be added on the same plot
DThe plot window would close
💡 Hint
Refer to key_moments about hold off effect and execution_table step 5
Concept Snapshot
plot(x,y) draws a graph.
hold on keeps current plot.
plot more graphs to add on same figure.
hold off stops adding plots.
Use to compare multiple data sets visually.
Full Transcript
This example shows how to plot multiple graphs on the same figure in MATLAB. First, we create x values and two y arrays for sine and cosine. We plot the sine curve. Then, we use 'hold on' to keep this plot visible. Next, we plot the cosine curve, which appears on the same graph. Finally, 'hold off' stops further plots from adding automatically. Without 'hold on', each new plot replaces the previous one. This technique helps compare multiple data sets visually in one figure.