Why does the axis 'Position' property not return the correct values after the size and position are changed?
7 次查看(过去 30 天)
显示 更早的评论
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')
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')
回答(1 个)
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.
- https://www.mathworks.com/help/releases/R2022a/matlab/ref/matlab.graphics.axis.axes-properties.html
- https://www.mathworks.com/help/releases/R2022a/matlab/ref/xlim.html
- https://www.mathworks.com/help/releases/R2022a/matlab/ref/ylim.html
Hope it resolves your query.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!