MATLAB图中轴标签和轴之间的距离 [英] Distance between axis label and axis in MATLAB figure
问题描述
我正在用MATLAB绘制一些数据,我想调整轴标签和轴本身之间的距离.但是,只需在标签的"Position"属性中添加一些内容,即可将标签移出图形窗口.是否有保证金"属性或类似内容?
I'm plotting some data with MATLAB and I'd like to adjust the distance between axis label and the axis itself. However, simply adding a bit to the "Position" property of the label makes the label move out of the figure window. Is there a "margin" property or something similar?
在上图中,我想增加数字与标签时间(s)"之间的距离,同时自动扩展图形的大小,以使标签不会超出范围.
In the above figure, I'd like to increase the distance between the numbers and the label "Time (s)" while automatically extending the figures size so that the label does not move out of bounds.
这就是我设置图形/轴的方式.
This is how I set up the figure / axis.
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 );
推荐答案
我编写了一个函数,该函数应该完全满足您的要求.它将轴保持在完全相同的大小和位置,将x标签向下移动,并将图形尺寸增加到足以显示标签的大小:
I wrote a function that should do exactly what you want. It keeps the axes at the exact same size and position, it moves the x-label down and increases the figure size to be large enough to show the 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
是轴手柄.
In this case offset
should be the absolute offset in pixels. If you want relative offsets, I think this function could easily be rewritten. hFig
is the figure handle and hAxes
the axes handle.
在调用函数之前,使用hFig = figure;
创建图形并使用hAxes = axes;
创建轴(然后像在问题中那样设置轴:set(hAxes,...)
).
create the figure using hFig = figure;
and the axes by hAxes = axes;
(then set up the axes like you did in the question: set(hAxes,...)
) before calling the function.
添加了将hAxes
的'Units'
和XLabel
分别更改回'normalized'和'data'的行.这样,在调整大小之后,图形将保持您想要的样子.
added the lines where the 'Units'
of hAxes
and the XLabel
are changed back to 'normalized' and 'data' respectively. That way the figure stays the way you want it after resizing.
修改了该功能以同时适用于X和Y标签.附加输入ax
应该是'x'
或'y'
.
modified the function to work for both X and Y labels. Additional input ax
should be 'x'
or 'y'
.
这篇关于MATLAB图中轴标签和轴之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!