Legend Positioning in figure w/Subplots

106 views (last 30 days)
Jamie Scanlan
Jamie Scanlan on 11 Aug 2018
Edited: jonas on 11 Aug 2018
I wish to move a legend of a subplot to one side of the whole figure, without manipulating the original subplot to compensate. I am trying to represent data from impacts on a structure, and I wish to display them in a report. I originally programmed the legends to be inside the subplots and were interactive to hide or show different data. However for print this is unneeded, and because of the size of the paper I am not able to maximize the figure to give myself room.
The abridged code is below:
for C = 1:c
figure('Name',char(scale(C))) %,'WindowState','maximized')
wf = subplot(2,3,[1 2 3]);
hold on
yyaxis left
envelope(wavetable{C})
ylabel('\it Level (Dimentionless)')
yyaxis right
xlabel('\it Length (samples)')
ylabel('\it Gradient (Dimentionless)')
plot(m{C},'DisplayName','Gradient')
title('Waveform Analysis')
wfL = legend(wf,'show','Location','westoutside');
wfL.ItemHitFcn = @legendCB_hideLine;
cepsum = 0;
for n = 1:length(IR{C})
td = subplot(2,3,4);
plot(td,time(1:length(IR{C}{n})),IR{C}{n},'DisplayName',sprintf('Wf: %i',n))
hold on
title('Time Domain')
xlabel('\it Time (s)')
ylabel('\it IR(t)')
axis tight
tdL = legend(fd,'show','Position',[1 1 0.4 0.5]); % <--- Position vector not correct.
tdL.ItemHitFcn = @legendCB_hideLine;
%<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
fd = subplot(2,3,5);
hold on
plot(fd,spect{C}{n}(1:1333,1),spect{C}{n}(1:1333,2),'DisplayName',sprintf('Wf: %i',n))
title('Frequency Domain')
xlabel('\it Frequency (Hz)')
ylabel('\it |IR(f)|')
axis tight
% fdL = legend(td,'show','Location','northeast');
% fdL.ItemHitFcn = @legendCB_hideLine;
ylim([0 inf]);
%<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
cd = subplot(2,3,6);
cepsum = cepsum + cepst{C}{n};
imagesc(cd,cepsum);
view(-90,90)
title('Cepstrum/MFCC')
xlabel('\it Filterbank Coefficent (Unit)')
ylabel('\it Timeframe (Unit)')
colorbar
caxis([-0.5 4])
end
path = sprintf('Data_Summary/X %u Y %0.2f.png',C,scale(C));
saveas(gcf,path)
end
I've tried using 'Position','[left bottom width height] but this puts the legend into the corner of the whole figure, and changing the values seems to do nothing.
I have used the outside locations, but they squeeze the selected subplot rather than just move beside the plot.
When I manipulate the legend interactively, I would like to place it like below.
But even using "Generate Code" to find how it represents the legend move doesn't work back in the script.
Is there a way of doing this programmatically, so every plot is the same?
Thanks, Jamie

Answers (1)

jonas
jonas on 11 Aug 2018
Edited: jonas on 11 Aug 2018
You want to add legend without the axes size being reduced? Easy, just add the legend inside the axes, e.g. southwest, as this won't change the axes size. Then change its position property to move it outside of the plot. I always recommend using the tight_subplot function from fileexchange to make subplots, especially when you are writing a paper and need to minimize empty space. It also let's you set the right margin so that you can fit the legend.
Example
figure;
set(gcf,'units','centimeters','position',[10 10 12 6])
[ha pos]=tight_subplot(1,2,0.05,[0.1 0.05],[0.05 0.2])
axes(ha(1))
h=plot([1 2;2 1;3 1]',[1 2;3 1; 4 5]')
axes(ha(2))
h=plot([1 2;2 1;3 1]',[1 2;3 1; 4 5]')
lh=legend(h,'location','east')
set(lh,'position',[.85 .5 .1 .1])
Image attached

Community Treasure Hunt

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

Start Hunting!