Why does the axis 'Position' property not return the correct values after the size and position are changed?

7 ビュー (過去 30 日間)
Peter
Peter 2022 年 11 月 30 日
回答済み: Abhaya 2024 年 10 月 14 日
I first plot data using the scatter function, and then set the x-axis and y-axis limits. I have the axis limits and style set to 'equal', so when the limits are changed, the size of the plotting area is automatically changed to maintain the correct aspect ratio.
The problem is that when I query the axis position and size after that, the result does not match the new size or position. Instead it returns the same size and position of the axis before the inner plot size was changed.
According to the documentation linked below, I believe I should be able to query the axis 'Position' property to get the size of the actual plot area (blue border in the diagram shown in the linked documentation page). But seems to either not be updating, or it's returning the size and position of some other object instead.
Below is a working example that reproduces my problem. If a breakpoint is set right before the xlim is changed, you can watch the actual size of the inner plot change. Note that the results of the 'Position' queries are [incorrectly] the same.
figure
hold on
axis equal
m = 8;
n = 5;
xv = (1:n);
xM = repmat(xv,m,1);
yv = (1:m);
yM = repmat(yv,n,1)';
c = rand(size(xM(:)));
scatter(xM(:),yM(:),[],c)
xspan = max(xM(:)) - min(xM(:));
yspan = max(yM(:)) - min(yM(:));
hax = gca;
psBeforeZoom = get(hax,'Position')
psBeforeZoom = 1×4
0.1300 0.1100 0.7750 0.8150
xlim([min(xM(:))-xspan*0.1 max(xM(:))+xspan*0.1])
ylim([min(yM(:))-yspan*0.1 max(yM(:))+yspan*0.1])
psAfterZoom = get(hax,'Position')
psAfterZoom = 1×4
0.1300 0.1100 0.7750 0.8150
  1 件のコメント
Peter
Peter 2022 年 12 月 1 日
I don't see any responses. Is this a bug? Is there any workaround for this issue?

サインインしてコメントする。

回答 (1 件)

Abhaya
Abhaya 2024 年 10 月 14 日
Hi Peter,
I understand you are getting the same results for Position property of axes even after setting the x-axis and y-axis limits.
This is because the xlim()” and ylim()” functions of MATLAB are used to display a specific range of data, by setting the limits on axes. However, these functions do not alter the position of the axes.
The Position property of the axes, which shows the designated plotting area within the figure window, remains unchanged during the process.
To visualize the complete area allotted to axes in the figure, please refer to the code given below.
ax=gca;
pos = ax.Position;
annotation("rectangle",pos,Color="blue",LineWidth=2)
The result can be visualized as the attached figure.
If you want to change the height and width of the axes, you can modify the Position property of the axes object. Please refer to code snippet given below for a better understanding.
ax=gca;
ax.Position=[ 0.1300 0.1 0.6 0.7]; %[left bottom width height]
For more information, please follow the MATLAB documentations given below.
Hope it resolves your query.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by