File Exchange

image thumbnail


version 1.6 (2.35 KB) by

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



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

function H=labelEdgeSubPlots(xl,yl,onlyBottom)

  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.

  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.
  H - a structure containing handles to the x and y axis labels

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

  H=labelEdgeSubPlots('beer [pints]','faux pas');
  H=labelEdgeSubPlots('beer [pints]','faux pas',1);
  %One can also do:

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

  Rob Campbell - January 2009

Comments and Ratings (12)

Jack Williams


Paul (view profile)

Rob: Very nice, thanks. Paul

Ya, the problem is my data is pairwise interaction so need to do xlabel and ylabel with 'A', 'B', 'C' respectively.

pie chart need to turn axis on to show xlabel, but it will also show axis line.

set 'Xtick' to [] removes the tick, but it still preserves the black axis line.

set 'XColor' into 'w' makes line invisible, but will also make the xlabel into white.

just wanna share my current finding.

And very useful code
rate it now :)

Rob Campbell

Rob Campbell (view profile)

I hadn't thought of pie charts since they don't have x and y axes. I think pie charts might require a more customised solution.

Dear Rob,

  This is excellent, but how do I make it work on pie chart?



Great function! thanks

Yuri K

Yuri K (view profile)

Nice function. Thanks!


thank you very much!!!!

Rob Campbell

Rob Campbell (view profile)



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
        L2 = ['h = ' num2str(-B) ' m'];
        L3 = ['m = ' num2str(a1,3)];
    elseif i>=12 && mod(i,2)==0
        L3 = ['m = ' num2str(a1,3)];

Best regards,

Rob Campbell

Rob Campbell (view profile)

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.


Dear Rob

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



And another correction to the help...


Fixed error in one of the exampled.


Add H1 line


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


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

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video