Search code examples
matlabmatlab-figure

Removing minor ticks in x-axis of semilogx plot in matlab


Here is an example of code

x = 0:1000;
y = log(x);
semilogx(x,y)

semilogx picture

I want to remove the small tick marks between 10^0 and 10^1 on the x-axis.

I tried:

set(gca,'XminorTick','off')

but it didn't work


Solution

  • There is really no good way to do this for good reason. It is always best to make it explicit that a log scale is used for a plot.

    If you really want, the easiest thing to do would be to perform a log transform on your data and plot it on a regular linear scale. Then specify custom tick labels to make it appear to be a logarithmic scale.

    %// Plot after performing log transform of your xdata
    plot(log10(x), y)
    
    %// Tick locations
    ticks = 0:3;
    
    %// Create custom tick labels
    labels = arrayfun(@(x)sprintf('10^%d', x), ticks, 'uni', 0);
    
    %// Update the ticks and ticklabels
    set(gca, 'xtick', ticks, 'XTickLabels', labels)
    

    enter image description here