Axis 'Innerposition' size doesn't match the one on the saved figure

4 次查看(过去 30 天)
I am saving a figure as an .svg file, using saveas.
The axis is set to 'axis equal' so that the aspect ratio is equal in x and y. I also set:
myplot = figure;
ax2 = axes;
scatter(xc,yc,[],c,'.');
axis equal
myplot.PaperUnits = 'points';
myplot.PaperPositionMode = 'manual';
myplot.PaperPosition = [0 0 1e3 1e3];
ax2.ActivePositionProperty = 'position'; %This should prioritise keeping the inner position as requested rather than the outer position
saveas(myplot, 'myfigure2.svg'],'svg');
When I look at my .svg file, my x and y axes are indeed the same lengths. (I am plotting the same number of units of each, so this is expected with axis equal).
However, when I request
>> ax2.InnerPosition
ans =
0.1300 0.1100 0.7750 0.8150
As you can see the width (0.77) and height (0.81) are not the same! This causes issues when I try to set the position of an annotation relative to the axes.
If I remove the 'axis equal' then the sizes do match what I see on file, however, the aspect ratio is now 1 which is not what I want.
Can anyone explain why the width and height are not the same, even though on the .svg file they are? And what I could do to fix it?
Thank you in avance :)

回答(1 个)

Prathamesh
Prathamesh 2025-5-2
编辑:Prathamesh 2025-5-5
I understand that while saving a figure as an SVG file, the aspect ratio is 1:1 after using “axis equal”. But the ratio changes after using “ax2.InnerPosition”.
When you use “axis equal” in MATLAB, it is ensured that one unit on the x-axis is the same length as one unit on the y-axis. To achieve this, MATLAB automatically adjusts the display area of the axes. As a result, the “InnerPosition” property (in normalized units) may not appear square, even though the axes look correct visually. This is why your SVG file shows equal axis lengths, but the “InnerPosition” values are not equal.
Because “InnerPosition” property does not show the true pixel size after `axis equal` is used, placing annotations using normalized axes coordinates may not work as expected.
To accurately position annotations after using `axis equal`, you can get the axes position in pixel units after the plot is rendered.
Here is an implementation of the same, in the given code snippet:
drawnow; % Ensures the graphics are updated
set(ax2, 'Units', 'pixels');
ax_pos = get(ax2, 'Position'); % [left bottom width height] in pixels

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by