Search code examples
matlabplotaxis-labels

Axis label in MATLAB for high precision values


I am trying to plot an array of data in MATLAB as follows:

ans = [8.1, 8.1+(10^-11) , 8.1+2*(10^-11) , 8.1+3*(10^-11)]
labels = [8.1, 8.1+(10^-11) , 8.1+2*(10^-11) , 8.1+3*(10^-11)]
plot([1,2,3,4],ans)

The above code produces this:-

plot

The problem is that the Y-axis displays the same value/number/label for all data points. How can I set the plot in a way to show these labels more precisely? (for example 8.1, 8.1+10^-11, ... or 8.10000000001 )

I tried the command below but it did not help.

set( gca, 'YTickLabel', get(gca, 'YTick'));

Can anyone please help improving this plot to reflect the differences between data points?


Solution

  • You can manually control the format of the Y-axis label in Matlab. So, the following code will work on the versions higher than Matlab2015b.

    res = [8.1, 8.1+(10^-11) , 8.1+2*(10^-11) , 8.1+3*(10^-11)];
    plot([1,2,3,4],res);
    ay=gca;
    ay.YAxis.TickLabelFormat = '%.12f';
    

    If you want to manually control the format of the Y-axis tick labels on your version, you should do something similar to

    res = [8.1, 8.1+(10^-11) , 8.1+2*(10^-11) , 8.1+3*(10^-11)];
    plot([1,2,3,4],res);
    ay=gca;
    currTicks=get(ay,'YTick');
    for i=1:size(currTicks,2)
        newTickLabels(i,1)=cellstr(sprintf('%.12f',currTicks(i)));
    end
    set(ay,'YTickLabel',newTickLabels);
    

    I did not test this code on Matlab2014b, but I don't see any functionality that it would be lacking (at least to my knowledge).

    In Matlab 2016a, there is no problem right "out of the box" by copying and pasting your code.