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
MathWorks Support Team 2018년 5월 17일
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