Search code examples
matlabmatlab-figure

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

Solution

  • 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.