Distance between axis label and axis in MATLAB figure
I know this has been answered and all, but this is (to some extent) a simpler way:
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])
I have included the pause commands, since my system will get ahead of itself in some weird way otherwise.
/Niels
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
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.
EDIT: 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.
EDIT2: 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.
EDIT3: modified the function to work for both X and Y labels. Additional input ax
should be 'x'
or 'y'
.
You can accomplish this by adjusting the position of the axis an xlabel. I also suggest using "normalized" units so your positioning does not depend on the data range. Here's an example:
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]);