MATLAB:在“轴相等”的情况下轴框的确切大小和位置?
MATLAB: The exact size and position of the axis box in the case of `axis equal`?
如何知道轴框的确切大小和位置(没有轴标签和数字)?例如,如果我使用
figure
contourf(x,y,u,100,'linestyle','none')
axis equal
set(gca,'position',[0.1,0.1,0.7,0.8]) %normalized units
轴frame/box的大小在图window调整大小(或使用axis equal
)的情况下发生变化,但get(gca,'position')
的值保持不变.例如:
figure
Z = peaks(20);
contourf(Z,10)
set(gca,'Units','pixels')
get(gca,'position')
axis equal
get(gca,'position')
答案=
0.1300 0.1100 0.7750 0.8150
在axis equal
之后轴框变了,但是get(gca,'position')
给出了相同的坐标:
答=
0.1300 0.1100 0.7750 0.8150
在 axis equal
的情况下,我需要这些来将颜色条与轴框对齐(它们之间有固定的间隙)。
当你调用axis equal
时,轴框纵横比是固定的,Position
属性被视为最大尺寸。当你调整图形大小时window,轴框会保持在Position
矩形的中心,但为了保持和之前一样的纵横比,它可能不会占据整个Position
矩形。
如果想让它占满整个Position
矩形,可以再调用axis equal
。 (这可能取决于您的 MATLAB 版本;它在 R2015b 中对我有用)。
对此也进行了更详细的讨论 on MATLAB Central。
要回答你原来的问题,有点复杂。您必须获得绘图框的纵横比(使用 pbaspect()
或轴 PlotBoxAspectRatio
属性)并计算出来:
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
单位中,你需要对其进行归一化:
fig_pos = get(get(ah,'Parent'),'Position');
box_position = box_position ./ fig_pos([3 4 3 4]);
如何知道轴框的确切大小和位置(没有轴标签和数字)?例如,如果我使用
figure
contourf(x,y,u,100,'linestyle','none')
axis equal
set(gca,'position',[0.1,0.1,0.7,0.8]) %normalized units
轴frame/box的大小在图window调整大小(或使用axis equal
)的情况下发生变化,但get(gca,'position')
的值保持不变.例如:
figure
Z = peaks(20);
contourf(Z,10)
set(gca,'Units','pixels')
get(gca,'position')
axis equal
get(gca,'position')
答案=
0.1300 0.1100 0.7750 0.8150
在axis equal
之后轴框变了,但是get(gca,'position')
给出了相同的坐标:
答=
0.1300 0.1100 0.7750 0.8150
在 axis equal
的情况下,我需要这些来将颜色条与轴框对齐(它们之间有固定的间隙)。
当你调用axis equal
时,轴框纵横比是固定的,Position
属性被视为最大尺寸。当你调整图形大小时window,轴框会保持在Position
矩形的中心,但为了保持和之前一样的纵横比,它可能不会占据整个Position
矩形。
如果想让它占满整个Position
矩形,可以再调用axis equal
。 (这可能取决于您的 MATLAB 版本;它在 R2015b 中对我有用)。
对此也进行了更详细的讨论 on MATLAB Central。
要回答你原来的问题,有点复杂。您必须获得绘图框的纵横比(使用 pbaspect()
或轴 PlotBoxAspectRatio
属性)并计算出来:
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
单位中,你需要对其进行归一化:
fig_pos = get(get(ah,'Parent'),'Position');
box_position = box_position ./ fig_pos([3 4 3 4]);