How do I overlay two plots upon the same axes?

조회 수: 370 (최근 30일)
Ben B
Ben B 2016년 2월 22일
댓글: Star Strider 2016년 2월 23일
I'm new to MatLab (so please excuse my language) and am attempting to overlay two line plots of vectors, as coded here (actual vectors a and b are 50 elements long):
a = [1, 2, 3]
b = [1, 3, 2]
compPlot = figure('Name', 'Comparison of stuff');
ax1 = axes('Parent', compPlot);
plot(ax1, a, 'Color', 'blue');
plot(ax1, b, 'Color', 'red');
title(ax1, 'Figure 3: Plot of stuff');
Basically, I'd like the blue and red lines to show up on the same graph. I know I can use a 'hold on' statement to plot them, but I was hoping to do it thru a "figure" statement since that seemed more elegant (and seems to make adjusting options easier and seems to be the more advanced and powerful way to do it).
Is there a way to do it using the plot(ax,...) method?
Thanks, Ben

채택된 답변

Star Strider
Star Strider 2016년 2월 22일
Use the hold function:
a = [1, 2, 3]
b = [1, 3, 2]
compPlot = figure('Name', 'Comparison of stuff');
ax1 = axes('Parent', compPlot);
plot(ax1, a, 'Color', 'blue');
hold on
plot(ax1, b, 'Color', 'red');
hold off
title(ax1, 'Figure 3: Plot of stuff');
  댓글 수: 2
Ben B
Ben B 2016년 2월 23일
Thanks! My final (simplified) code:
compPlot = figure('Name', 'Comparison of Stuff');
ax1 = axes('Parent', compPlot);
hold(ax1, 'on');
plot(ax1, a, 'Color', 'blue');
plot(ax1, b, 'Color', 'red');
title(ax1, 'Figure 3: Plot of Stuff');
legend('Thing 1','Thing 2');
hold(ax1, 'off');
Star Strider
Star Strider 2016년 2월 23일
My pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by