%% Aspect Ratio Properties
% 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.
%% Data Aspect Ratio
% The DataAspectRatio property controls the ratio of the axis scales. For example, to display a surface plot of a mathematical expression MATLAB selects a data aspect ratio that emphasizes the function’s values:

[X,Y] = meshgrid((-2:.15:2),(-4:.3:4));
Z = X.*exp(-X.^2 - Y.^2);
surf(X,Y,Z)
set(gca,'BoxStyle','full','Box','on')

%
% The daspect function returns the actual value of the DataAspectRatio property.

daspect

% 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 surface 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].

daspect([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.
%
%% Plot Box Aspect Ratio
% 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. The pbaspect function to returns the value of the PlotBoxAspectRatio:

pbaspect

% Notice that MATLAB rescaled the plot box to accommodate the graph using the specified DataAspectRatio.
% The PlotBoxAspectRatio property controls the shape of the axes plot box. By default, MATLAB sets this property to [1 1 1] and adjusts the DataAspectRatio property so that graphs fill the plot box or until reaching a constraint.
% 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, MATLAB changes the axis limits to satisfy the two constraints placed on the axes.

daspect([1 1 1])
pbaspect([1 1 1])

%
%% Adjusting Axis Limits
% The axes also has properties for setting the x-, y-, and z-axis limits. However, specifying the axis limits with the PlotBoxAspectRatio and DataAspectRatio properties overconstrains the axes. For example, this command specifies axis limits that conflict with the PlotBoxAspectRatio value.

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

%
% If you query the plot box aspect ratio, you can see that the PlotBoxAspectRatio value changed to accommodate the axis limits.

pbaspect

% Copyright 2019 The MathWorks, Inc.