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

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Star Strider
Star Strider il 13 Mar 2022
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')
.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Tag

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by