MATLAB: The exact size and position of the axis box in the case of `axis equal`?
Asked Answered
B

1

2

How to know the exact size and position of the axis box (without axis labels and numbers)? For example, if I use

figure
contourf(x,y,u,100,'linestyle','none')
axis equal
set(gca,'position',[0.1,0.1,0.7,0.8]) %normalized units

The size of the axis frame/box is varyed in the case of the figure window resizing (or using axis equal), but the value of get(gca,'position') remains unchanged. For example:

figure
Z = peaks(20);
contourf(Z,10)
set(gca,'Units','pixels')

get(gca,'position')
axis equal 
get(gca,'position')

ans =

0.1300    0.1100    0.7750    0.8150

after axis equal, the axis box is changed, but get(gca,'position') gives the same coordinates: ans =

0.1300    0.1100    0.7750    0.8150

I need these to align the colorbar to the axis box (with fixed gap between them) in the case of axis equal.

Bellwort answered 8/9, 2016 at 16:19 Comment(2)
Your question was unclear and the following explains more easily what you fnid: figure Z = peaks(20); contourf(Z,10) set(gca,'Units','pixels') get(gca,'position') axis equal get(gca,'position')Ivoryivorywhite
I have added your example to my question. Is it more clear now?Bellwort
I
1

When you call axis equal, the axis box aspect ratio is fixed and the Position property is treated as a maximum size. When you resize the figure window, the axis box will remain centered in the Position rectangle, but in order to maintain the same aspect ratio as before, it may not take up the entire Position rectangle.

If you want it to take up the entire Position rectangle, you can call axis equal again. (this may depend on your MATLAB version; it worked for me in R2015b).

This is also discussed in a bit more detail on MATLAB Central.

To answer your original question, it's a bit complicated. You'd have to get the plot box aspect ratio (using pbaspect() or the axes PlotBoxAspectRatio property) and figure it out:

ah = gca();
% Get the axes Position rectangle in units of pixels
old_units = get(ah,'Units');
set(ah,'Units','pixels');
pos = get(ah,'Position');
set(ah,'Units',old_units);
% Figure the PlotBox and axes Position aspect ratios
pos_aspectRatio = pos(3) / pos(4);
box_aspect = pbaspect(ah);
box_aspectRatio = box_aspect(1) / box_aspect(2);
if (box_aspectRatio > pos_aspectRatio)
    % PlotBox is wider than the Position rectangle
    box_height = pos(3) / box_aspectRatio;
    box_dy = (pos(4)-box_height) / 2;
    box_position = [pos(1), pos(2)+box_dy, pos(3), box_height];
else
    % PlotBox is taller than the Position rectangle
    box_width = pos(4) * box_aspectRatio;
    box_dx = (pos(3)-box_width) / 2;
    box_position = [pos(1)+box_dx, pos(2), box_width, pos(4)];
end

Note that this will give you the box position in pixels; if you want it in the normalized units that are the axes default, you'll need to normalize it:

fig_pos = get(get(ah,'Parent'),'Position');
box_position = box_position ./ fig_pos([3 4 3 4]);
Impersonality answered 8/9, 2016 at 18:55 Comment(7)
Thank you very much. Unfortunately your code does not give exact position of the axis box. Moreover it leads to negative values.Bellwort
Are you using a 3-D plot? It becomes a lot more complicated in that caseImpersonality
I use surf with view(2). Thus it is a 2D plot.Bellwort
In that case, I'm not sure why you're getting negative values. Can you post a more complete example that includes how you're generating the plot?Impersonality
Sorry your code is correct. I found an error in my code.Bellwort
However, it is strange that we can't get these coordinates directly from the graph without relatively cumbersome calculations.Bellwort
I agree 100%. MATLAB's plotting functions can be very frustrating, and are definitely one of its weak points.Impersonality

© 2022 - 2024 — McMap. All rights reserved.