MATLAB:在“轴相等"的情况下,轴箱的确切大小和位置? [英] MATLAB: The exact size and position of the axis box in the case of `axis equal`?

查看:99
本文介绍了MATLAB:在“轴相等"的情况下,轴箱的确切大小和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道轴箱的确切大小和位置(没有轴标签和数字)?例如,如果我使用

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

在调整图形窗口大小(或使用axis equal)的情况下,轴框架/框的大小会有所不同,但get(gca,'position')的值保持不变.例如:

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

axis equal之后的

,轴盒被更改,但是get(gca,'position')给出了相同的坐标: ans =

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

axis equal的情况下,我需要这些来使颜色条与轴箱对齐(它们之间具有固定的间隙).

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

推荐答案

调用axis equal时,轴盒纵横比是固定的,并且Position属性被视为最大大小.调整图形窗口的大小时,轴框将保持在Position矩形的中心,但是为了保持与以前相同的长宽比,它可能不会占用整个Position矩形.

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.

如果您希望它占用整个Position矩形,则可以再次调用axis equal. (这可能取决于您的MATLAB版本;它在R2015b中对我有用).

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).

要回答您的原始问题,这有点复杂.您必须获取绘图框的长宽比(使用pbaspect()或轴PlotBoxAspectRatio属性)并弄清楚:

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

请注意,这将使您以像素为单位的框位置;如果要使用默认的normalized轴单位,则需要对其进行归一化:

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]);

这篇关于MATLAB:在“轴相等"的情况下,轴箱的确切大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆