Adding legend for subplot when none of the subplots include all legend entries
110 views (last 30 days)
Show older comments
Hi folks, I am trying to create a subplot where the points are coloured by year, and the years have the same colour in each subplot. Through careful control of the colours I have been able to achieve this, but I only want one legend for the figure, and none of the subplots contain all the years. I cannot figure out (excuse the pun!) how to create just one legend that has all the years (2011-2016). I can easily position one legend at the bottom of the subplot, that's not the problem. Here is the code to make the figure with individual legends (which I don't want in the end) to show you what I mean, along with the code. Thank you!
st_colours=[0, 0.4470, 0.7410;0.8500, 0.3250, 0.0980; 0.9290, 0.6940, 0.1250; 0.4940, 0.1840, 0.5560; 0.4660, 0.6740, 0.1880; 0.3010, 0.7450, 0.9330; 0.6350, 0.0780, 0.1840];
figure15 = figure();
for m=1:12
subplot(4,3,m)
if m==1 || m==2 || m==3 || m==4 || m==5 || m==6 || m==8 || m==11 || m==12 %Month
for r=1:5 %The row this time
sc(m,r)=scatter(QQG_Amm3M_T_split(m).months.NOAA_ONI(r),QQG_Amm3M_T_split(m).months.mean_Tmelt(r),25,'filled');hold on;
end
end
if m==7 || m==9 || m==10
for r=1:4 %The row this time
sc(m,r)=scatter(QQG_Amm3M_T_split(m).months.NOAA_ONI(r),QQG_Amm3M_T_split(m).months.mean_Tmelt(r),25,'filled');hold on;
end
end
if m==11 || m==12
for r=1:5 %The row this time
sc(m,r).MarkerFaceColor=st_colours(r,:);
end
elseif m==1 || m==2 || m==3 || m==4 || m==5 || m==6 || m==8
for r=1:5 %The row this time
sc(m,r).MarkerFaceColor=st_colours(r+1,:);
end
else
for r=1:4 %The row this time
sc(m,r).MarkerFaceColor=st_colours(r+1,:);
end
end
legend(string(QQG_Amm3M_T_split(m).months.mean_Year));
title(Month_labels(m));
xlim([-2 2.5]);
ylim([0 1]);
end
2 Comments
Adam Danz
on 29 Sep 2020
You can simplify the if m==1 || m==2 || m==3 || m.... conditions by using ismember instead.
if ismember(m, [1,2,3, . . .])
Accepted Answer
Adam Danz
on 29 Sep 2020
Edited: Adam Danz
on 29 Sep 2020
All you need are the handles to each object you want to include in the legend and the handles can come from different subplots within the same figure. But the legend is a child of only one subplot and can be repositioned relative to that subplot.
% DEMO
figure()
colors = [winter(3);summer(3);autumn(3);copper(3)];
subPlotNames = 'ABCD';
h = gobjects(3,4);
for i = 1:4
ax = subplot(1,5,i);
h(:,i) = plot([0,1],[.66;.5;.33].*[1,1],'LineWidth',4);
text([.1,.1,.1],[.66;.5;.33],{'1' '2' '3'},'VerticalAlignment', 'Bottom')
set(h(:,i), {'DisplayName'}, compose('%s %d',subPlotNames(i),1:3)')
title(subPlotNames(i))
end
set(h(:), {'Color'}, mat2cell(colors,ones(12,1),3))
lg = legend(h(:));
lg.Position(1:2) = [.8 .3];
This method also works with TiledLayout but requries r2020b for the last line which repositions the legend.
Another method I've used a lot is to use the DisplayName property of line objects to assign the legend string to all line objects within a figure even if the legend string & line line properties associations are redundant (e.g. more than 1 orange marker named "2012"). Then use legendUnq() from the file exchange to eliminate the objects with redundant legend strings. After applying DisplayName, you just need to call this after assigning all objects to the plot,
legend(legendUnq())
3 Comments
More Answers (1)
Mario Malic
on 29 Sep 2020
Legend command is in the loop, therefore it is generated for each subplot. If you want to have it on particular month, you can put this within condition
if m == 1 % Jan
legend(string(QQG_Amm3M_T_split(m).months.mean_Year));
end
Or you can put it just outside of loop, and legend will be on December.
5 Comments
Mario Malic
on 29 Sep 2020
Having an additional scatter command for the January, you can have enough entries in the legend, and for it make the legend, but it's not a proper solution.
scatter(nan, nan);
Or maybe you could add an entry to your structure (nan, nan) to December and adjust the code, as it's easier to append the last value rather than the first.
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!