Legend inside for loop
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I want to generate single legend outside for the figure window for all subplots. I am doing plots inside a loop like this:
t,x and y are given datas
for i=1:10
figure()
subplot(2,2,i)
plot(x,y)
hold on
plot(t,y)
subplot(2,2,i)
plot(x,y-y')
hold on
% ........
legend('1','2,','3')
end
But i am still getting the legend on all plots.
any help is appreciated
Dan
6 件のコメント
回答 (1 件)
Walter Roberson
2022 年 8 月 4 日
for i=1:10
figure()
subplot(2,2,i)
H(3*i-2) = plot(x, y, 'displayname', i + "A");
hold on
H(3*i-1) = plot(t, y, 'displayname', i + "B");
subplot(2,2,i)
H(3*i) = plot(x, y-y', 'displayname', i + "C");
hold on
% ........
end
legend(H)
Are you wanting only 3 entries in the legend, total? If so then you would want to be explicit about the line colors, and you would legend(H(1:3))
4 件のコメント
Walter Roberson
2022 年 8 月 4 日
x = 1:10; y = rand(size(x));
for i=1:10
subplot(2,5,i)
H(3*i-2) = plot(x, y, 'displayname', i + "A");
hold on
H(3*i-1) = plot(x, y, 'displayname', i + "B");
H(3*i) = plot(x, y-y', 'displayname', i + "C");
hold off
% ........
end
legend(H)
What size is y? If y is a vector then y - y' creates a square matrix in which each element of y is subtracted from each other element of y; the result of plotting that would be multiple lines.
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!