Area plot common legend

2 ビュー (過去 30 日間)
Hasan Isomitdinov
Hasan Isomitdinov 2019 年 1 月 29 日
コメント済み: Star Strider 2019 年 1 月 29 日
Hi!
I am trying to plot my data with an area plot and I have several subplots for several countries. I just wanted to know how to set a common legend for all subplots describing each color of the plot. Thanks!
  2 件のコメント
Luna
Luna 2019 年 1 月 29 日
What have you done so far?
Hasan Isomitdinov
Hasan Isomitdinov 2019 年 1 月 29 日
figure(1)
for i=1:11
subplot(4,3,i)
area(dates,[global(:,i),regional(:,i),local(:,i)],'FaceColor','flat')
a11 = sprintf('%s',country{i});
title(a11);
legend({'World','Regional','Local'},'Location','SouthOutside');
end
But this is giving me a legend for each plot separately.

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

採用された回答

Star Strider
Star Strider 2019 年 1 月 29 日
Some version of this idea could work:
dates = 1:5; % Create Data
G = rand(5, 11); % Create Data
R = rand(5, 11); % Create Data
L = rand(5, 11); % Create Data
country = num2cell('A'+(0:10)); % Create Data
figure(1)
for i=1:12
if i < 12
subplot(4,3,i)
area(dates,[G(:,i),R(:,i),L(:,i)],'FaceColor','flat')
a11 = sprintf('%s',country{i});
title(a11);
elseif i == 12
subplot(4,3,i)
i = 11;
area(dates,[G(:,i),R(:,i),L(:,i)],'FaceColor','flat')
set(gca,'Position',[7.5 1.25 1 1]*0.1)
set(gca, 'Visible','off')
legend({'World','Regional','Local'},'Location','best');
end
end
Run this first. (I am using R2018b.)
Then experiment with your own data to get the result you want.
  2 件のコメント
Hasan Isomitdinov
Hasan Isomitdinov 2019 年 1 月 29 日
Thank you for your response!
Here is a little tweaked version I opted for instead which is giving me the result I wanted with a row legend underneath the figure. Any suggestions on optimizing this line would still be appreciated.
clear all;
dates = 1:5; % Create Data
G = rand(5, 11); % Create Data
R = rand(5, 11); % Create Data
L = rand(5, 11); % Create Data
country = num2cell('A'+(0:10)); % Create Data
figure(1)
for i=1:11
subplot(4,4,i)
area(dates,[G(:,i),R(:,i),L(:,i)],'FaceColor','flat')
a11 = sprintf('%s',country{i});
title(a11);
end
legend=columnlegend(3,{'World','Regional','Country-specific'});
set(legend,'Position',[1.1 1.3 7 1]*0.1);
Star Strider
Star Strider 2019 年 1 月 29 日
My pleasure.
I cannot run that because the ‘columnlegend’ variable does not exist in the code you posted. Also, ‘columnlegend’ is not a MATLAB function.
If my Answer helped you solve your problem, please Accept it!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by