%% Control Axes Layout
%% Axes Position Properties
% The Axes object has several properties that control the axes size and the layout of titles and axis labels within a figure. By default, the axes automatically resizes when you resize the figure. You can control the resize behavior and how the axes accommodates the titles and labels by setting these properties.
% OuterPosition - Boundaries of the axes, including the title, labels, and a margin. This property is a vector of the form [left bottom width height], where left and bottom indicate the distance from the lower left corner of the figure to the lower left corner of the axes, and width and height indicate the axes dimensions.
% Positive - Boundaries of the axes, excluding the title, labels, and a margin. This property is a vector of the form [left bottom width height].
% TightInset - Margins added to the width and height of the Position property, specified as a vector of the form [left bottom right top]. When you add axis labels and a title, the values update to accommodate the text. The size of the boundary defined by the Position and TightInset properties include all graph text.
% ActivePositionProperty - Position property to use when resizing the figure containing the axes, specified as either 'outerposition' (the default) or 'position'.
% Units - Position units. The units must be set to 'normalized' (the default) to enable automatic axes resizing.
%
%% Position and Margin Boundaries (2-D View)
% This figure shows the areas defined by the OuterPosition values (red), the Position values (blue), and the Position expanded by the TightInset values (magenta) for axes in a 2-D view.

figure 
plot(1:10)
title('My Title')
xlabel('My x-axis Label')
ylabel('My y-axis Label')
ax = gca;

outpos = ax.OuterPosition;
annotation('rectangle',outpos,'Color','red','LineWidth',4) % OuterPosition
pos = ax.Position;
annotation('rectangle',pos,'Color','blue','LineWidth',2) % Position
ti = ax.TightInset;
pos_ti = [pos(1)-ti(1), pos(2)-ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
annotation('rectangle',pos_ti,'Color','magenta','LineWidth',2) % Position + TightInset

%
%% Position and Margin Boundaries (3-D View)
% This figure shows the areas defined by the OuterPosition values (red), the Position values (blue), and the Position expanded by the TightInset values (magenta) for axes in a 3-D view.

figure
surf(peaks)
title('My Title')
xlabel('My x-axis Label')
ylabel('My y-axis Label')
zlabel('My z-axis label')
ax = gca;

outpos = ax.OuterPosition;
annotation('rectangle',outpos,'Color','red','LineWidth',4) % OuterPosition
pos = ax.Position;
annotation('rectangle',pos,'Color','blue','LineWidth',2) % Position
ti = ax.TightInset;
pos_ti = [pos(1)-ti(1), pos(2)-ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
annotation('rectangle',pos_ti,'Color','magenta','LineWidth',2) % Position + TightInset

%
%% Automatic Axes Resize
% If you resize the figure and if the axes position is normalized to the figure (the default behavior), then the axes automatically resizes to fit the new figure size. When the axes resizes, the OuterPosition, Position, and TightInset property values might change. The ActivePositionProperty value controls whether to ensure that the OuterPosition values or the Position values are preserved. To avoid the possibility that your title or label text is cut off, leave the ActivePositionProperty property value set to 'outerposition' (the default).
% For example, create a figure with two axes. Set the ActivePositionProperty to 'outerposition' for the first axes and to 'position' for the second axes. Then resize the figure so that it is much smaller. Notice that the text for the second axes does not have enough room and is cut off.

fig = figure; 
ax1 = axes('Position',[0.13 0.11 0.3347 0.815]); 
ax1.ActivePositionProperty = 'outerposition'; 
plot(ax1,1:10) 
title(ax1,'Preserve OuterPosition') 
xlabel(ax1,'My x-axis Label') 

ax2 = axes('Position',[0.5703 0.11 0.3347 0.815]); 
ax2.ActivePositionProperty = 'position'; 
plot(ax2,1:10) 
title(ax2,'Preserve Position') 
xlabel(ax2,'My x-axis Label') 

%

fpos = fig.Position;
fig.Position = fpos/1.5;

%
%
% Copyright 2012 The MathWorks, Inc.