Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the position of the Xtick labels matlab

I want to shift the x ticks labels downwards in this figure:fig

I'm not sure how to do this?

This is the script I'm using:

y=[0.5093 0.8526 0.9171];
x=[0 1600 1100];
hand =plot(y, 'ob-');
set(gca, 'XTick',1:3, 'XTickLabel',{'no interference' '1600' '1100'})
set(hand, 'LineWidth', 4);
set(hand, 'MarkerSize', 30);
set(findobj('type','text'),'FontSize',25);
set(gca,'FontSize',25);
set(findobj('type','axes'),'FontSize',25);
h=get(gca,'Title');
set(h,'FontSize',20);
like image 689
Tak Avatar asked May 20 '13 04:05

Tak


People also ask

How do you rotate a Xtick label in MATLAB?

Rotate x-Axis Tick Labels for Specific Axes Call the nexttile function to create the axes objects ax1 and ax2 . Plot into each of the axes. Then rotate the x-axis tick labels for the lower plot by specifying ax2 as the first input argument to xtickangle .

How do you assign Y-axis labels in MATLAB?

Label y-Axis of Specific AxesCall the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes, and create an y-axis label for the top plot.


2 Answers

Following the example from this mathworks solution, you can use the text function to add labels in any position you wish.

Increase the value of delta for a larger gap between x tick labels and x axis.

EDIT: Added custom control of yticks: the value of stp changes the step between each tick. Obviously a more general solution would identify the end-points of the tick range automatically as well.

figure(1), clf

% set data as your example
y=[0.5093 0.8526 0.9171];
x=[0 1600 1100];
Xt=1:length(x);
hand =plot(y, 'ob-');
set(gca, 'XTick',Xt);

stp=0.05;
Yt=0.5:stp:0.95;
set(gca, 'ytick', Yt)

% Reduce the size of the axis so that all the labels fit in the figure.
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .2, pos(3) .7])

ax = axis; % Current axis limits
axis(axis); % Set the axis limit modes (e.g. XLimMode) to manual
Yl = ax(3:4); % Y-axis limits
Xl = ax(1:2);

% Place the text labels -- the value of delta modifies how far the labels 
% are from the axis.
delta=0.1;
t = text(Xt, Yl(1)*ones(1,length(x))-delta, {'no interference' '1600' '1100'});
%set(t, 'HorizontalAlignment','left','VerticalAlignment','top')
set(t, 'HorizontalAlignment','center','VerticalAlignment','middle')

% Remove the default labels
set(gca,'XTickLabel','')

% and continue with your other settings as required
set(hand, 'LineWidth', 4);
set(hand, 'MarkerSize', 30);
set(findobj('type','text'),'FontSize',25);
set(gca,'FontSize',25);
set(findobj('type','axes'),'FontSize',25);
h=get(gca,'Title');
set(h,'FontSize',20);

The text function has lots of options that you can configure.

like image 126
Bonlenfum Avatar answered Oct 03 '22 00:10

Bonlenfum


I think what most people want is something that works in 2-3 lines of code, nevermind-ing a quick&dirty approach.

This is undocumented (credits go to here) but just works:

    % adjust ticklabels away from axes
    a=gca;
    a.XRuler.TickLabelGapOffset = 8;
    a.YRuler.TickLabelGapOffset = 8;

tested with Matlab 2019a

like image 28
user2305193 Avatar answered Oct 02 '22 22:10

user2305193