Problem with axis equal

376 次查看(过去 30 天)
I have a problem with the following code:
inputfigure = figure('Name','Test','Numbertitle','off','Units','centimeters','Position',[5 2 40 23],'Visible','on');
sub2 = subplot('Position',[0.05 0.45 0.2 0.5],'Xdir','reverse');
hold on
grid on
axis equal % this causes the problem
sub2.Layer = 'top';
sub2.Units = 'centimeters';
sub2.Position(4) = sub2.Position(3); % this should make the plot square
The last line should make the plot square, but it only works if I remove axis equal. Why is that? I know I can put 'axis equal' afterwards, but I'd still like to know ;) (As a sidenote, it seems weird that 'subplot' does not accept 'units','centimeters' on creation although the lower level 'axes' command does).
  2 个评论
DGM
DGM 2021-3-21
axis equal
sets the 'PlotBoxAspectRatio' property to a calculated value and sets 'PlotBoxAspectRatioMode' property to 'manual' instead of auto. Changing the position property afterwards doesn't change the aspect ratio since it's in manual mode. Are you sure you don't mean to do something like
axis square
instead?
broken_arrow
broken_arrow 2021-3-22
编辑:broken_arrow 2021-3-22
Thanks for explaining. I know about axis square, I just wanted to improve my understanding ;). So what changes if I modify the 'position' property afterwards? The position value is still updated, but what does that nominal change of size actually do if it doesn't really influence the plot size?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-3-22
axis equal sets the axes DataAspectRatio to [1 1 1], so that one physical unit in each direction corresponds to one data unit.
It also sets the PlotBoxAspectRatio, which controls the relative ratios of drawing the axes. You can have different sizes of axes drawn while the data to physical ratio might be the same for all three axes.
The PlotBoxAspectRatio is set so as to retain the same data span on the shorter of what x or y was, but accounting for the data units being made uniform. So after axis equal, the visual appearance of the axes is left the same, but the longer of the two distances (in data units) has been adjusted to account for the units now being uniform.
When you set the Position property, the DataAspectRatio is left at [1 1 1], but the PlotBoxAspectRatioMode is set to 'manual' and the PlotBoxAspectRatio reflects the size you set.
Setting axis equal does not constrain the axis Position to be equal x and y span; it constrains the data aspect ratio; and when you set the axis Position manually, it keeps the data aspect ratio in effect.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by