我写了一个应该完全符合你想要的功能 . 它使轴保持完全相同的大小和位置,向下移动x标签并将图形尺寸增大到足以显示标签:
function moveLabel(ax,offset,hFig,hAxes)
% get figure position
posFig = get(hFig,'Position');
% get axes position in pixels
set(hAxes,'Units','pixels')
posAx = get(hAxes,'Position');
% get label position in pixels
if ax=='x'
set(get(hAxes,'XLabel'),'Units','pixels')
posLabel = get(get(hAxes,'XLabel'),'Position');
else
set(get(hAxes,'YLabel'),'Units','pixels')
posLabel = get(get(hAxes,'YLabel'),'Position');
end
% resize figure
if ax=='x'
posFigNew = posFig + [0 -offset 0 offset];
else
posFigNew = posFig + [-offset 0 offset 0];
end
set(hFig,'Position',posFigNew)
% move axes
if ax=='x'
set(hAxes,'Position',posAx+[0 offset 0 0])
else
set(hAxes,'Position',posAx+[offset 0 0 0])
end
% move label
if ax=='x'
set(get(hAxes,'XLabel'),'Position',posLabel+[0 -offset 0])
else
set(get(hAxes,'YLabel'),'Position',posLabel+[-offset 0 0])
end
% set units back to 'normalized' and 'data'
set(hAxes,'Units','normalized')
if ax=='x'
set(get(hAxes,'XLabel'),'Units','data')
else
set(get(hAxes,'YLabel'),'Units','data')
end
end
在这种情况下, offset 应该是像素的绝对偏移量 . 如果你想要相对偏移,我认为这个函数很容易被重写 . hFig 是图形句柄, hAxes 轴处理 .
编辑:在调用函数之前,使用 hFig = figure; 创建图形,使用 hAxes = axes; 创建图形(然后像在问题中一样设置轴: set(hAxes,...) ) .
EDIT2:添加 hAxes 和 XLabel 的 'Units' 分别变回'normalized'和'data'的行 . 这样,在调整大小后,这个数字会保持你想要的方式 .
EDIT3:修改了该功能,适用于X和Y标签 . 附加输入 ax 应为 'x' 或 'y' .