필터 지우기
필터 지우기

How to plot 2 plots in one for loop with each plot having a legend

조회 수: 3 (최근 30일)
I want to plot two graphs in one for loop where each graph has multiple curves. Please see below for an example of what I mean. I would like to plot two graphs each with the 5 different curves. At the moment the second plot just overwrites the first.
x = 1:1:10;
y = zeros(10,5);
figure;
for i = 1:5
for j = 1:10
y(j,i) = i*x(j);
z(j,i) = 2*i*x(j);
end
% First plot with 5 curves
plot(x(:),y(:,i))
% Second plot with 5 curves
plot(x(:),z(:,i))
hold on
end
hold off
Thanks for your help!

채택된 답변

Star Strider
Star Strider 2022년 3월 13일
Try something like this —
x = 1:1:10;
y = zeros(10,5);
figure;
Ax1 = axes;
hold on
figure
Ax2 = axes;
hold on
for i = 1:5
for j = 1:10
y(j,i) = i*x(j);
z(j,i) = 2*i*x(j);
end
% First plot with 5 curves
plot(Ax1,x(:),y(:,i))
% Second plot with 5 curves
plot(Ax2,x(:),z(:,i))
end
title(Ax1,'First Five Curves')
legend(Ax1,"Curve "+string(1:5), 'Location','best')
title(Ax2,'Second Five Curves')
legend(Ax2,"Curve "+string(1:5), 'Location','best')
.

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by