How to create an overall legend for subplots with the SUBAXIS function?

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 4 月 22 日
コメント済み: Sim 2022 年 4 月 22 日
Hi, how can I create an overall legend by using the subaxis function?
The following example does not work:
x = -10:10;
city = {'London', 'Paris', 'Berlin','Madrid'};
season = {'summer','winter'};
colors = {'blue','red','black','magenta'};
for i = 1 : 4
subaxis(2,2,i);
y1 = (rand(1,21)-0.5)*20;
y2 = (rand(1,21)-0.5)*10;
hold on
p1(i) = plot(x, y1, 'Color', colors{i}, 'LineStyle','--');
p2(i) = plot(x, y2, 'Color', colors{i}, 'LineStyle','-');
hold off
p1(i).DisplayName = [city{i} ', ' season{1}];
p2(i).DisplayName = [city{i} ', ' season{2}];
end
lgd = legend;
lgd.Position = 'northeastoutside';

採用された回答

Matt J
Matt J 2022 年 4 月 22 日
編集済み: Matt J 2022 年 4 月 22 日
x = -10:10;
city = {'London', 'Paris', 'Berlin','Madrid'};
season = {'summer','winter'};
colors = {'blue','red','black','magenta'};
for i = 1 : 4
ax(i)=subaxis(2,2,i);
y1 = (rand(1,21)-0.5)*20;
y2 = (rand(1,21)-0.5)*10;
hold on
p1(i) = plot(x, y1, 'Color', colors{i}, 'LineStyle','--');
p2(i) = plot(x, y2, 'Color', colors{i}, 'LineStyle','-');
hold off
p1(i).DisplayName = [city{i} ', ' season{1}];
p2(i).DisplayName = [city{i} ', ' season{2}];
end
for i=1:4
ax(i).Position([1,3])=ax(i).Position([1,3])*0.8; %make room for legend
end
lgd = legend;
lgd.Position(1:2)=[0.75,0.4];
  3 件のコメント
Matt J
Matt J 2022 年 4 月 22 日
I don't have a clear picture of what you're seeing. Maybe try clearing and/or resizing the figure.
Sim
Sim 2022 年 4 月 22 日
Oh, solved..!!
Your solution works well, I just had an annotation, and the "EdgeColor" of the annotation was still "black" (the default color)....
I solved it just by setting "EdgeColor" as "none", as shown here:
annotation('textbox','String',str,...
'Position',ax(i).Position,...
'EdgeColor','none');

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by