我写了一个函数,应该做你想要的。它保持在完全相同的大小和位置的轴,它使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:增加了的和XLabel
分别变回“归一化”和“数据”的行。这样,这个数字在调整大小后保持您想要的样子。
EDIT3:修改了X和Y标签的功能。其他输入ax
应该是'x'
或'y'
。
解决y轴有什么显着的区别?在这种情况下,我想情节本身需要移动,对吧? – Niko 2013-02-25 09:42:49
我认为代码很容易转换为在y轴上工作。我现在正在乘公共汽车,所以现在不能这样做,但我会在今晚晚些时候(几个小时内)看看它。 – ThijsW 2013-02-25 09:54:15
由于某种原因,此方法无法通过例如。在x轴上有50个像素偏移量。在我的设置中,标签然后被切成两半。然而,在几个像素的理想范围内,这个功能就像一个魅力,所以非常感谢你! – Niko 2013-02-25 17:11:58