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

15 次查看(过去 30 天)
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.).

采纳的回答

MathWorks Support Team
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 个评论
Walter Roberson
Walter Roberson 2017-11-28
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))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2007a

Community Treasure Hunt

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

Start Hunting!

Translated by