## Documentation Center |

On this page… |
---|

Default Aspect Ratio Selection |

The `axis` command works
by setting various axes object properties. You can set these properties
directly to achieve precisely the effect you want.

Property | Description |
---|---|

DataAspectRatio | Sets the relative scaling of the individual axis data
values. Set |

DataAspectRatioMode | In |

PlotBoxAspectRatio | Sets the proportions of the axes plot box (set |

PlotBoxAspectRatioMode | In |

Position | Defines the location and size of the axes with a four-element
vector: [ |

XLim, YLim,
ZLim | Sets the minimum and maximum limits of the respective axes. |

XLimMode,
YLimMode, ZLimMode | In |

By default, MATLAB automatically determines values for all of these properties (i.e., all the modes are auto) and then applies stretch-to-fill. You can override any property's automatic operation by specifying a value for the property or setting its mode to manual. The value you select for a particular property depends primarily on what type of data you want to display.

Much of the data visualized with MATLAB is either

Numerical data displayed as line or mesh plots

Representations of real-world objects (e.g., a dump truck or a section of the earth's topography)

In the first case, it is generally desirable to select axis limits that provide good resolution in each axis direction and to fill the available space. Real-world objects, on the other hand, need to be represented accurately in proportion, regardless of the angle of view.

There are two key elements to the default behavior — normalizing the axes size to the window size and stretch-to-fill.

The axes `Position` property
specifies the location and dimensions of the axes. The third and fourth
elements of the `Position` vector (`width` and `height`)
define a rectangle in which MATLAB draws the axes (indicated
by the dotted line in the following pictures). MATLAB stretches
the axes to fill this rectangle.

The default value for the axes `Units` property
is `normalized` to the parent figure dimensions.
This means the shape of the figure window determines the shape of
the position rectangle. As you change the size of the window, MATLAB reshapes
the position rectangle to fit it.

The view is the 2-D projection of the plot box onto the screen.

As you can see, reshaping the axes to fit into the figure window can change the aspect ratio of the graph. MATLAB applies stretch-to-fill so the axes fill the position rectangle and in the process can distort the shape. This is generally desirable for graphs of numeric data, but not for displaying objects realistically.

MATLAB surface plots are well suited for visualizing mathematical
functions of two variables. For example, to display a mesh plot of
the function z=xe^{(-x2–y2)}evaluated
over the range -2 ≤ *x* ≤ 2, -4 ≤ *y* ≤
4, use the statements

[X,Y] = meshgrid([-2:.15:2],[-4:.3:4]); Z = X.*exp(-X.^2 - Y.^2); mesh(X,Y,Z)

The MATLAB default property values are designed to

Select axis limits to span the range of the data (

`XLimMode`,`YLimMode`, and`ZLimMode`are set to`auto`).Provide the highest resolution in the available space by setting the scale of each axis independently (

`DataAspectRatioMode`and the`PlotBoxAspectRatioMode`are set to`auto`).Draw axes that fit the position rectangle by adjusting the

`CameraViewAngle`and then stretch-to-fill the axes if necessary.

To maintain a particular shape, you can specify the size of the axes in absolute units such as inches, which are independent of the figure window size. However, this is not a good approach if you are writing a MATLAB file that you want to work with a figure window of any size. A better approach is to specify the aspect ratio of the axes and override automatic stretch-to-fill.

In cases where you want a specific aspect ratio, you can override stretching by specifying a value for these axes properties:

`DataAspectRatio`or`DataAspectRatioMode``PlotBoxAspectRatio`or`PlotBoxAspectRatioMode``CameraViewAngle`or`CameraViewAngleMode`

The first two sets of properties affect the aspect ratio directly.
Setting either of the mode properties to `manual` simply
disables stretch-to-fill while maintaining all current property values.
In this case, MATLAB enlarges the axes until one dimension of
the position rectangle constrains it.

Setting the `CameraViewAngle` property disables
stretch-to-fill, and also prevents MATLAB from readjusting the
size of the axes if you change the view.

It is important to understand how properties interact with each
other, in order to obtain the results you want. The `DataAspectRatio`, `PlotBoxAspectRatio`,
and the *x*-, *y*-, and *z*-axis
limits (`XLim`, `YLim`, and `ZLim` properties)
all place constraints on the shape of the axes.

The `DataAspectRatio` property
controls the ratio of the axis scales. For a mesh plot of the function z=xe^{(-x2–y2)} evaluated
over the range -2 ≤ *x* ≤ 2, -4 ≤ *y* ≤
4

[X,Y] = meshgrid([-2:.15:2],[-4:.3:4]); Z = X.*exp(-X.^2 - Y.^2); mesh(X,Y,Z)

the values are

get(gca,'DataAspectRatio') ans = 4 8 1

This means that four units in length along the *x*-axis
cover the same data values as eight units in length along the *y*-axis
and one unit in length along the *z*-axis. The
axes fill the plot box, which has an aspect ratio of [1 1 1] by default.

If you want to view the mesh plot so that the relative magnitudes
along each axis are equal with respect to each other, you can set
the `DataAspectRatio` to `[1 1 1]`.

set(gca,'DataAspectRatio',[1 1 1])

Setting the value of the `DataAspectRatio` property
also sets the `DataAspectRatioMode` to `manual` and
overrides stretch-to-fill so the specified aspect ratio is achieved.

Looking at the value of the `PlotBoxAspectRatio` for the graph in the previous section shows that it has
now taken on the former value of the `DataAspectRatio`.

get(gca,'PlotBoxAspectRatio') ans = 4 8 1

MATLAB has rescaled the plot box to accommodate the graph
using the specified `DataAspectRatio`.

The `PlotBoxAspectRatio` property controls
the shape of the axes plot box. MATLAB sets this property to `[1
1 1]` by default and adjusts the `DataAspectRatio` property
so that graphs fill the plot box if stretching is on, or until reaching
a constraint if stretch-to-fill has been overridden.

When you set the value of the `DataAspectRatio` and
thereby prevent it from changing, MATLAB varies the `PlotBoxAspectRatio` instead.
If you specify both the `DataAspectRatio` and the `PlotBoxAspectRatio`, MATLAB is
forced to change the axis limits to obey the two constraints you have
already defined.

Continuing with the mesh example, if you set both properties,

set(gca,'DataAspectRatio',[1 1 1],... 'PlotBoxAspectRatio',[1 1 1])

MATLAB changes the axis limits to satisfy the two constraints placed on the axes.

MATLAB enables you to set the axis limits to the values
you want. However, specifying a value for `DataAspectRatio`, `PlotBoxAspectRatio`,
and the axis limits overconstrains the axes definition. For example,
it is not possible for MATLAB to draw the axes if you set these
values:

set(gca,'DataAspectRatio',[1 1 1],... 'PlotBoxAspectRatio',[1 1 1],... 'XLim',[-4 4],... 'YLim',[-4 4],... 'ZLim',[-1 1])

In this case, MATLAB ignores the setting of the `PlotBoxAspectRatio` and
determines its value automatically. These particular values cause
the `PlotBoxAspectRatio` to return to its calculated
value.

get(gca,'PlotBoxAspectRatio') ans = 4 8 1

MATLAB can now draw the axes using the specified `DataAspectRatio` and
axis limits.

Sometimes projecting a 3-D surface onto an *x-*, *y-*,
or *z*-axis can aid visualization. To do this,
you might change the aspect ratio, in order to make space for the
projection. The following example illustrates how to do this:

Create an

*x*-*y*grid and*z*value for it:[x,y] = meshgrid([-2:.2:2]); Z = x.*exp(-x.^2-y.^2);

Plot the surface in 3-D; annotate with a colorbar and axis labels:

surf(x,y,Z,gradient(Z)) colorbar xlabel('X-AXIS') ylabel('Y-AXIS') zlabel('Z-AXIS')

Use

`axis`to change the`Ymax`value in to 3, stretching the plot in one direction:axis([-2 2 -2 3 -0.5 0.5]) %

Regrid the surface, setting all Y value equal to

`3`:y = 3*ones(21);

Plaster a plot of the surface onto the Y-axis:

hold on surf(x,y,Z,gradient(Z))

If you want to display an object so that it looks realistic, you need to change MATLAB defaults. For example, this data defines a wedge-shaped patch object.

patch('Vertices',vertex_list,'Faces',vertex_connection,... 'FaceColor','w','EdgeColor','k') view(3)

However, this axes distorts the actual shape of the solid object
defined by the data. To display it in correct proportions, set the `DataAspectRatio`.

set(gca,'DataAspectRatio',[1 1 1])

The
units are now equal in the *x*-, *y*-,
and *z*-directions and the axes is not being stretched
to fill the position rectangle, revealing the true shape of the object.

Was this topic helpful?