Control Axes Layout

Axes Position-Related Properties

The Axes object has several properties that control the axes size and the layout of titles and axis labels within a figure.

  • OuterPosition — Outer boundary of the axes, including the title, labels, and a margin. Specify this property as a vector of the form [left bottom width height]. The left and bottom values indicate the distance from the lower left corner of the figure to the lower left corner of the outer boundary. The width and height values indicate the outer boundary dimensions.

  • Position — Boundary of the inner axes where plots appear, excluding the title, labels, and a margin. Specify this property as a vector of the form [left bottom width height].

  • TightInset — Margins added to the width and height of the Position property values, specified as a vector of the form [left bottom right top]. This property is read-only. When you add axis labels and a title, MATLAB® updates the values to accommodate the text. The size of the boundary defined by the Position and TightInset properties includes all graph text.

  • PositionConstraint — Position property preserved when the Axes object changes size, specified as either 'outerposition' or 'innerposition'.

  • Units — Position units. The units must be set to 'normalized' (the default) to enable automatic axes resizing. When the position units are a unit of length, such as inches or centimeters, then the Axes object is a fixed size.

Position and Margin Boundaries

This figure shows a 2-D view of the axes areas defined by the OuterPosition values (red), the Position values (blue), and the Position expanded by the TightInset values (magenta).

This figure shows a 3-D view of the axes areas defined by the OuterPosition values (red), the Position values (blue), and the Position expanded by the TightInset values (magenta).

Controlling Automatic Resize Behavior

Some scenarios can trigger the Axes object to automatically resize. For example, interactively resizing the figure or adding a title or axis labels activates automatic resizing. Sometimes, the new axes size cannot satisfy both the Position and OuterPosition values, so the PositionConstraint property indicates which values to preserve. Specify the PositionConstraint property as one of these values:

  • 'outerposition' — Preserve the OuterPosition value. Use this option when you do not want the axes or any of the surrounding text to extend beyond a certain outer boundary. MATLAB adjusts the size of the inner area of the axes (where plots appear) to try to fit the contents within the outer boundary.

  • 'innerposition' — Preserve the InnerPosition value. Use this option when you want the inner area of the axes to remain a certain size within the figure. This option sometimes causes text to run off the figure.

Usually, leaving the PositionConstraint property set to 'outerposition' is preferable. However, an overly long axes title or labels can shrink the inner area of your axes to a size that is hard to read. In such a case, keeping the inner axes to a specific size can be preferable, even if the surrounding text runs off the figure.

For example, create a figure with two axes and specify the same width and height for each axes position. Set the PositionConstraint property to 'outerposition' for the upper axes and to 'innerposition' for the lower axes. Notice that in the upper axes, the inner area shrinks to accommodate the text, but the text does not run outside the figure. In the lower axes, the size of the inner area is preserved, but some of the text is cut off.

Note

The following code uses the PositionConstraint property, which is new starting in R2020a. If you are using an earlier release, set the ActivePositionProperty to either 'outerposition' or 'position'.

figure; 
ax1 = axes('Position',[0.13 0.58 0.77 0.34]);
ax1.PositionConstraint = 'outerposition';
% R2019b and earlier: ax1.ActivePositionProperty = 'outerposition';
plot(ax1,1:10)
title(ax1,'Preserve OuterPosition')
yticklabels(ax1,{'My incredibly descriptive, excessively wordy, and overly long label',...
    'label 2','label 3'})

ax2 = axes('Position',[0.13 0.11 0.77 0.34]);
ax2.PositionConstraint = 'innerposition';
% R2019b and earlier: ax2.ActivePositionProperty = 'position';
plot(ax2,1:10)
title(ax2,'Preserve Position')
yticklabels(ax2,{'My incredibly descriptive, excessively wordy, and overly long label',...
    'label 2','label 3'})

Stretch-to-Fill Behavior

By default, MATLAB stretches the axes to fill the available space. This “stretch-to-fill” behavior can cause some distortion. The axes might not exactly match the data aspect ratio, plot box aspect ratio, and camera-view angle values stored in the DataAspectRatio, PlotBoxAspectRatio, and CameraViewAngle properties. The “stretch-to-fill” behavior is enabled when the DataAspectRatioMode, PlotBoxAspectRatioMode, and CameraViewAngleMode properties of the Axes object are set to 'auto'.

If you specify the data aspect ratio, plot box aspect ratio, or camera-view angle, then the “stretch-to-fill” behavior is disabled. When the “stretch-to-fill” behavior is disabled, MATLAB makes the axes as large as possible within the available space and strictly adheres to the property values so that there is no distortion.

For example, this figure shows the same plot with and without the “stretch-to-fill” behavior enabled. The dotted line shows the available space as defined by the Position property. In both versions, the data aspect ratio, plot box aspect ratio, and camera-view angle values are the same. However, in the left plot, the stretching introduces some distortion.

Stretch-to-fill enabled (some distortion)Stretch-to-fill disabled (no distortion)

See Also

Functions

Properties

Related Topics