2013-02-19 440 views
11

我正在用MATLAB绘制一些数据,我想调整轴标签和轴本身之间的距离。但是,只需在标签的“位置”属性中添加一点,标签就会移出图形窗口。有没有“保证金”财产或类似的东西?MATLAB中轴标签和轴之间的距离图

enter image description here

在上述图中,我想增加号码和标签“时间(s)”同时自动延伸的数字大小,使得标签不移动出界之间的距离。

这是我如何设置图形/轴。

figure; 
set(gca, ... 
    'Box'   , 'off'      , ... 
    'LooseInset' , get(gca, 'TightInset') * 1.5 , ... 
    'TickDir'  , 'in'       , ... 
    'XMinorTick' , 'off'      , ... 
    'YMinorTick' , 'off'      , ... 
    'TickLength' , [.02 .02]     , ... 
    'LineWidth' , 1       , ... 
    'XGrid'  , 'off'      , ... 
    'YGrid'  , 'off'      , ... 
    'FontSize' , 18       ); 

回答

8

我写了一个函数,应该做你想要的。它保持在完全相同的大小和位置的轴,它使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'

+0

解决y轴有什么显着的区别?在这种情况下,我想情节本身需要移动,对吧? – Niko 2013-02-25 09:42:49

+0

我认为代码很容易转换为在y轴上工作。我现在正在乘公共汽车,所以现在不能这样做,但我会在今晚晚些时候(几个小时内)看看它。 – ThijsW 2013-02-25 09:54:15

+0

由于某种原因,此方法无法通过例如。在x轴上有50个像素偏移量。在我的设置中,标签然后被切成两半。然而,在几个像素的理想范围内,这个功能就像一个魅力,所以非常感谢你! – Niko 2013-02-25 17:11:58

8

您可以通过调整xlabel轴的位置来实现此目的。我还建议使用“标准化”单位,以便您的定位不取决于数据范围。这里是一个例子:

figure 
plot(rand(1,10)) 

set(gca, 'Units', 'Normalized'); 
pos = get(gca, 'Position'); 
offset = 0.1; 
set(gca, ... 
    'Box'   , 'off'      , ... 
    'LooseInset' , get(gca, 'TightInset') * 1.5 , ... 
    'TickDir'  , 'in'       , ... 
    'XMinorTick' , 'off'      , ... 
    'YMinorTick' , 'off'      , ... 
    'TickLength' , [.02 .02]     , ... 
    'LineWidth' , 1       , ... 
    'XGrid'  , 'off'      , ... 
    'YGrid'  , 'off'      , ... 
    'FontSize' , 18       , ... 
    'Position' , pos + [0, offset, 0, -offset]); 

h = xlabel('Time (s)'); 
set(h, 'Units', 'Normalized'); 
pos = get(h, 'Position'); 
set(h, 'Position', pos + [0, -offset, 0]); 
+0

这似乎是一个很好的方法,谢谢。但是,如果我设置'offset = 0.01;',则标签位于图形的边框上,因此将其切成两半。 – Niko 2013-02-19 21:33:45

+0

@妮科那是真的。你必须四处游玩以获得抵消的良好价值。您可能还需要轴和xlabel的不同偏移量。 – shoelzer 2013-02-19 21:35:19

+1

最后4行是我需要调整标签位置。 – 2015-10-06 23:47:56

4

我知道这已经回答了所有,但是这是(在某种程度上)一个简单的方法:

relative_offset = 1.5; 
close all; 
figure(99);clf 
plot(rand(1,10)) 
xlabel('The x-axis') 
xh = get(gca,'XLabel'); % Handle of the x label 
pause(0.2) 
set(xh, 'Units', 'Normalized') 
pause(0.2) 
pos = get(xh, 'Position'); 
set(xh, 'Position',pos.*[1,relative_offset,1]) 

我已经包含暂停命令,因为我的系统会提前拿到自己在一些奇怪的否则的话。

/Niels

相关问题