Multiple plots (hold on) in MATLAB - Time & Space Complexity
When we use multiple plots with hold on in MATLAB, we add several drawing steps. We want to understand how the time to draw grows as we add more plots.
How does the time needed change when we plot more lines on the same figure?
Analyze the time complexity of the following code snippet.
x = linspace(0, 2*pi, 1000);
hold on;
for k = 1:n
y = sin(k*x);
plot(x, y);
end
hold off;
This code plots n sine waves on the same figure, each with 1000 points.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runsntimes, each time callingplotto draw a line. - How many times: The loop repeats exactly
ntimes, once per sine wave.
Each additional plot adds a similar amount of work because it draws another line with 1000 points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 plot calls, each drawing 1000 points |
| 100 | 100 plot calls, each drawing 1000 points |
| 1000 | 1000 plot calls, each drawing 1000 points |
Pattern observation: The total work grows directly with the number of plots n. Doubling n roughly doubles the drawing time.
Time Complexity: O(n)
This means the time to draw grows in a straight line with the number of plots you add.
[X] Wrong: "Adding more plots won't affect the time much because the points are fixed at 1000."
[OK] Correct: Each plot call still draws all 1000 points, so more plots mean more total points drawn, increasing time linearly.
Understanding how repeated drawing commands affect performance helps you write efficient visualization code and shows you can think about how work grows with input size.
What if we increased the number of points per plot from 1000 to 10,000? How would the time complexity change?
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
