MATLAB: Area plot common legend

area plotlegendsimulink

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!

Best Answer

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.