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

2 次查看(过去 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