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

6 views (last 30 days)
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 :)

Answers (1)

Prathamesh
Prathamesh on 2 May 2025
Edited: Prathamesh on 5 May 2025
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

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!