Why does the formatting of my Y-axis tick labels change to exponential format when I plot my data?

24 views (last 30 days)
I am plotting x and y data as follows:
x = [1 2 3]
y = [10000 20000 30000]
plot(x,y)
However, once the figure is plotted the Y-axis tick labels are in exponential format 1 ,2 ,3 e+4 . I want the Y-axis tick labels to be in the same format as my original 'y' vector (i.e. 10000, 20000, etc.).

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 May 2018
There are two possible workarounds:
1) You can set the Y-ticklabels explicity as follows:
x = [ 1 2 3 ];
y = [10000 20000 30000];
plot(x,y)
set(gca,'YTick',y)
set(gca,'XTick',x)
Yvalues = get(gca,'YTick');
set(gca,'YTickLabel',Yvalues);
2) You could also use the SPRINTF function to format your tick labels using the format of your choice. The 'XTickLabel' or 'YTickLabel' property of the axis would then use those strings as tick labels.
x=[1 2 3];
y=[10000 20000 30000];
plot(x,y)
set(gca,'XTickLabel',sprintf('%3.1f|',x))
set(gca,'XTick',x)
set(gca,'YTickLabel',sprintf('%4.1e|',y))
set(gca,'YTick',y)
For more information on the available formats, consult the documentation for the SPRINTF function:
doc sprintf
  1 Comment
Walter Roberson
Walter Roberson on 28 Nov 2017
In R2017b you can instead
ax = gca;
ax.YRuler.Exponent = 0;
without setting the labels.
However, this might fail for some log plots.
The above code can also be fixed as,
set(gca,'XTickLabel', sprintfc('%3.1f',x))
set(gca,'YTickLabel', sprintfc('%4.1e',y))

Sign in to comment.

More Answers (0)

Products


Release

R2007a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!