Suplabel.m deletes legends from multiple subplots

2 vues (au cours des 30 derniers jours)
Valerie
Valerie le 2 Sep 2014
Commenté : Geoff Hayes le 3 Avr 2015
I am using suplabel.m from the file exchange to label some plots that have multiple subplots. However, whenever I want more than one of those subplots to have its own legend, suplabel removes all but the last legend specified. Has anyone encountered this and figured out a fix?
I should note this is "suplabel" not "suptitle" since they're easy to confuse. Also, I'm using 2013a.
Thank you.
Below is the suplabel.m code for reference:
% Author: Ben Barrowes <barrowes@alum.mit.edu>
%modified 3/16/2010 by IJW to make axis behavior re "zoom" on exit same as
%at beginning. Requires adding tag to the invisible axes
currax=findobj(gcf,'type','axes','-not','tag','suplabel');
if nargin < 3
supAxes=[.08 .08 .84 .84];
ah=findall(gcf,'type','axes');
if ~isempty(ah)
supAxes=[inf,inf,0,0];
leftMin=inf; bottomMin=inf; leftMax=0; bottomMax=0;
axBuf=.04;
set(ah,'units','normalized')
ah=findall(gcf,'type','axes');
for ii=1:length(ah)
if strcmp(get(ah(ii),'Visible'),'on')
thisPos=get(ah(ii),'Position');
leftMin=min(leftMin,thisPos(1));
bottomMin=min(bottomMin,thisPos(2));
leftMax=max(leftMax,thisPos(1)+thisPos(3));
bottomMax=max(bottomMax,thisPos(2)+thisPos(4));
end
end
supAxes=[leftMin-axBuf,bottomMin-axBuf,leftMax-leftMin+axBuf*2,bottomMax-bottomMin+axBuf*2];
end
end
if nargin < 2, whichLabel = 'x'; end
if nargin < 1, help(mfilename); return; end
if ~isstr(text) | ~isstr(whichLabel)
error('text and whichLabel must be strings')
end
whichLabel=lower(whichLabel);
ax=axes('Units','Normal','Position',supAxes,'Visible','off','tag','suplabel');
if strcmp('t',whichLabel)
set(get(ax,'Title'),'Visible','on')
title(text);
elseif strcmp('x',whichLabel)
set(get(ax,'XLabel'),'Visible','on')
xlabel(text);
elseif strcmp('y',whichLabel)
set(get(ax,'YLabel'),'Visible','on')
ylabel(text);
elseif strcmp('yy',whichLabel)
set(get(ax,'YLabel'),'Visible','on')
ylabel(text);
set(ax,'YAxisLocation','right')
end
for k=1:length(currax), axes(currax(k));end % restore all other axes
if (nargout < 2)
return
end
if strcmp('t',whichLabel)
h=get(ax,'Title');
set(h,'VerticalAlignment','middle')
elseif strcmp('x',whichLabel)
h=get(ax,'XLabel');
elseif strcmp('y',whichLabel) | strcmp('yy',whichLabel)
h=get(ax,'YLabel');
end

Réponse acceptée

Geoff Hayes
Geoff Hayes le 2 Sep 2014
Valerie - I was able to reproduce the same behaviour. In one of the comments (by Jeff) at suplabel, he said
I added the following after line 39 in order to preserve subplot legends:
currax=[currax;findobj(gcf,'tag','legend')];
I added this line just after line 6 (of suplabel.m) as
currax=findobj(gcf,'type','axes','-not','tag','suplabel');
currax=[currax;findobj(gcf,'tag','legend')];
I then re-ran the following to test
close all
figure;
x=-2*pi:0.0001:2*pi;
y=sin(x);
subplot(1,2,1)
plot(x,y,'r')
legend('plot 1')
subplot(1,2,2)
plot(x,y,'b')
hold on;
plot(x,y+1,'g')
legend('plot 2','plot 3')
[ax1,h1]=suplabel('super X label');
Both subplots appeared with their legends intact, along with the the super title/label.
  1 commentaire
Geoff Hayes
Geoff Hayes le 3 Avr 2015
Valerie's answer moved here
Thank you!
I read through the comments on the suplabel page several times (including twice after you pointed it out) and only then did I spot the comment dealing with subplots. So thank you for being willing to patiently point to what should have been obvious :-)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by