フィルターのクリア

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

3 ビュー (過去 30 日間)
William Greenway
William Greenway 2022 年 3 月 13 日
コメント済み: Star Strider 2022 年 3 月 13 日
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')
.
  2 件のコメント
William Greenway
William Greenway 2022 年 3 月 13 日
Perfect!
Star Strider
Star Strider 2022 年 3 月 13 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by