Search code examples
matlabmatlab-figure

How to show all custom axes in Matlab?


I'm writing a code to plot a graph with custom ticks (not ordered), this way:

axes1 = axes('Parent',figure,'XTickLabel',col1);
box(axes1,'on');
hold(axes1,'all');
plot(col2,'b*')

col1 and col2 are extracted from a matrix (each one have 1000 values). First values of col1 are: 120, 147, 644, 940 ...

The resulting image is the following: enter image description here

As you can see X axis follows the order I presented, but it should be 1000 values, not the 11 first values... How can I set X axis to col1 when col1 is not increasing (they are random numbers) and how can I make it fit with the points I want to show (col2)?


Solution

  • You also have to adjust the XTick property of the axes, since it will only use as many of your labels as there are ticks, drawing them in order from the beginning of your col1 array.

    You have two general options: show all the labels (which will get messy since you have 1000) or show a sampling of labels. Here's an example of the first way:

    % Sample data:
    col1 = num2cell('a':'z');
    col2 = 1:26;
    
    axes1 = axes('Parent', figure, 'XTick', 1:numel(col1), 'XTickLabel', col1);
    box(axes1, 'on');
    hold(axes1, 'all');
    plot(col2, 'b*');
    

    enter image description here


    And here's an example of the second way, showing only every fifth label:

    axes1 = axes('Parent', figure, 'XTick', 1:5:numel(col1), 'XTickLabel', col1(1:5:end));
    box(axes1, 'on');
    hold(axes1, 'all');
    plot(col2, 'b*');
    

    enter image description here