Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between axis number and axis in MATLAB figure

Tags:

plot

matlab

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.

Is there a possibility to do that?

Thanks in advance, Joe

like image 705
Josef Pörnbacher Avatar asked Jan 23 '26 07:01

Josef Pörnbacher


1 Answers

Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:

clc
clear
close all

x = 1:20;

hPlot = plot(x,sin(x));

set(gca,'xaxisLocation','top');

set(gca,'XTickLabel',[]); %// Clear current XTickLabel

ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.

for k = 2:2:20
    text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end

Giving this:

enter image description here

Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

like image 146
Benoit_11 Avatar answered Jan 25 '26 22:01

Benoit_11