首页 文章

MATLAB图中轴标签与轴之间的距离

提问于
浏览
11

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

enter image description here

在上图中,我想增加数字和标签“Time(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                           );

3 回答

  • 8

    我知道这已经得到了回答,但这在某种程度上是一种更简单的方式:

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

    我已经包含了暂停命令,因为我的系统会以某种奇怪的方式超越自己 .

    /尼尔斯

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

    我写了一个应该完全符合你想要的功能 . 它使轴保持完全相同的大小和位置,向下移动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:添加 hAxesXLabel'Units' 分别变回'normalized'和'data'的行 . 这样,在调整大小后,这个数字会保持你想要的方式 .

    EDIT3:修改了该功能,适用于X和Y标签 . 附加输入 ax 应为 'x''y' .

相关问题