Code covered by the BSD License  

Highlights from
labelEdgeSubPlots

4.5

4.5 | 2 ratings Rate this file 5 Downloads (last 30 days) File Size: 2.3 KB File ID: #26332
image thumbnail

labelEdgeSubPlots

by Rob Campbell

 

09 Jan 2010 (Updated 22 Jan 2010)

Automatically apply x and y labels to subplots only along the edges.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

function H=labelEdgeSubPlots(xl,yl,onlyBottom)

  Purpose
  If all subplots have the same quantities on the x and y axes then
  there's no point labeling all of them. Often it looks neater to
  simply have y labels only on the plots along the left hand
  edge and x labels only on the plots along the bottom. This
  function does this automatically for the current figure.

  Inputs
  xl - a string specifying what to label the x axes.
  yl - a string specifying what to label the y axes.
  onlyBottom - by default this is zero and the function adds an
               x-axis to appropriate plots from the penultimate row
               if the bottom row of plots is incomplete. set
               onlyBottom to 1 to suppress this behaviour.
               * in addition: onlyBottom can be a vector of axis handles to
                    be processsed.
  Outputs
  H - a structure containing handles to the x and y axis labels

  Example
  clf
  for i=1:5
  subplot(2,3,i)
  x=[0:10]; y=1+0.01*x.^3+randn(size(x))*0.2;
  plot(x,y,'ok'), xlabel('will be removed')
  end

  H=labelEdgeSubPlots('beer [pints]','faux pas');
  %or:
  H=labelEdgeSubPlots('beer [pints]','faux pas',1);
  %One can also do:
  set(H.xlabels,'color','red','fontweight','bold')

  %proccess only some subplots
  ax=[];
  for i=1:25
  subplot(5,5,i), box on
  if mod(i,5)==1, ax=[gca,ax]; end
  end
  labelEdgeSubPlots('X','Y',ax);

  Rob Campbell - January 2009

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
15 Jun 2010 GERMÁN

Dear Rob

how would be the case: ylabel different in top and bottom in the subplot??

15 Jun 2010 Rob Campbell

Top and bottom of what? Do you mean different rows should have different y labels? If so, that is beyond the scope of this function because different people will have different requirements. However, I the function returns the handles to the x and y labels. It would be trivial for you to use those handles to further modify your sub-plots as desired. I think this is the "correct" way to do things.

15 Jun 2010 GERMÁN

yes, I mean different rows should have different y labels. I have two rows and six columns.The top row has the name of "wall", the bottom row has the name of "center."
I like that "wall" and "center" appear only in the first colunma (ylabel).

if i<12 && mod(i,2)==1
        subplot(2,6,(i/2+0.5));
        loglog(freq([1:longi(i)],i),power([1:longi(i)],i),'b-'...
            ,freq([1:longi(i)],i),powerfit([1:longi(i)],i),'k-');
        L2 = ['h = ' num2str(-B) ' m'];
        L3 = ['m = ' num2str(a1,3)];
        title({L2;L3},'FontSize',8);
    elseif i>=12 && mod(i,2)==0
        subplot(2,6,(i/2+1));
        loglog(freq([1:longi(i)],i),power([1:longi(i)],i),'b-'...
            ,freq([1:longi(i)],i),powerfit([1:longi(i)],i),'k-');
        L3 = ['m = ' num2str(a1,3)];
        title({L3},'FontSize',8);
    end

Best regards,

15 Jun 2010 Rob Campbell

Easy:
H=labelEdgeSubPlots('X','WALL');
set(H.ylabels(1),'String','CENTRE')

15 Jun 2010 GERMÁN

thank you very much!!!!

13 Jul 2011 Yuri Kotliarov

Nice function. Thanks!

21 Dec 2011 sharsad

Great function! thanks

Please login to add a comment or rating.
Updates
12 Jan 2010

Now skips axes which are Matlab-generated legends or colorbars. Please let me know if it gets confused by other figure child elements.

12 Jan 2010

Returns label handles.
3rd input can now also be a vector of axis handles to be labeled.

22 Jan 2010

Add H1 line

Tag Activity for this File
Tag Applied By Date/Time
plotting Rob Campbell 11 Jan 2010 11:17:30
label Rob Campbell 11 Jan 2010 11:17:30
subplot Rob Campbell 11 Jan 2010 11:17:30

Contact us at files@mathworks.com