How to plot two lines on the same graph

조회 수: 92 (최근 30일)
Mariah Villanueva
Mariah Villanueva 2015년 2월 16일
댓글: Image Analyst 2015년 2월 16일
So these are my variables: x = [1:10] y = [1:10] z = [2:11]
And I know to plot them each separately, I type this for one: plot(x,y,'b*')
And this for the other: plot(x,z,'b-')
But how do I write it so that both show up on the same graph?

답변 (2개)

Image Analyst
Image Analyst 2015년 2월 16일
plot(x,y,'b*')
hold on;
plot(x,z,'b-')

Franck Kamga
Franck Kamga 2015년 2월 16일
plot(x,y,'b*') hold on plot(x,z,'b-') hold off
Don't forget to put hold off in case you want to create a separate graph not on the same plot.
  댓글 수: 1
Image Analyst
Image Analyst 2015년 2월 16일
You don't need to take hold off the first plot to keep it from applying on the second plot. Hold applies on a plot-specific basis. When you plot on the separate graph, it does not have "hold on" because you applied it on the first graph. See example:
subplot(2,1,1);
plot(rand(10, 1), 'b*-');
hold on
% Add a red line.
plot(rand(10, 1), 'r*-');
subplot(2,1,2);
plot(rand(10, 1), 'b*-');
% Plot red line. Blows away blue line because hold is off
plot(rand(10, 1), 'r*-');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by