How to plot 2 plots in one for loop with each plot having a legend
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
William Greenway
il 13 Mar 2022
Commentato: Star Strider
il 13 Mar 2022
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!
0 Commenti
Risposta accettata
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')
.
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!