【问题标题】:Distance between axis label and axis in MATLAB figureMATLAB图中轴标签和轴之间的距离
【发布时间】:2013-02-04 16:22:15
【问题描述】:

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

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

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

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

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

    我已经包含了暂停命令,因为否则我的系统会以某种奇怪的方式超前。

    /尼尔斯

    【讨论】:

      【解决方案2】:

      我写了一个函数,它应该完全符合你的要求。它将轴保持在完全相同的大小和位置,向下移动 x-label 并将图形大小增加到足够大以显示标签:

      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'Units'XLabel 分别改回“规范化”和“数据”的行。这样,在调整大小后,图形将保持您想要的样子。

      EDIT3:修改了函数以适用于 X 和 Y 标签。附加输入 ax 应为 'x''y'

      【讨论】:

      • 在处理 y 轴时有什么明显的区别吗?我想在这种情况下,情节本身需要以某种方式移动,对吧?
      • 我认为代码很容易转换为在 y 轴上工作。我现在在公共汽车上,所以现在不能这样做,但我会在今晚晚些时候(几个小时内)看看它
      • 由于某种原因,此方法失败,例如在 x 轴上偏移 50 像素。在我的设置中,标签被切成两半。但是,在所需的几个像素范围内,这个功能就像一个魅力,非常感谢你!
      • 奇怪.. 以后有时间我会试着看看它。但我很高兴它可以满足您的需要!
      【解决方案3】:

      您可以通过将轴的位置调整为 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]);
      

      【讨论】:

      • 这似乎是一个好方法,谢谢。但是,如果我设置offset = 0.01;,则标签位于图形的边框上,因此被切成两半。
      • @Niko 这是真的。您必须四处玩耍才能获得良好的抵消价值。您可能还需要轴和 xlabel 的不同偏移量。
      • 最后 4 行是我调整标签位置所需的全部内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多