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

15 visualizaciones (últimos 30 días)
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.).

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 17 de Mayo de 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 comentario
Walter Roberson
Walter Roberson el 28 de Nov. de 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))

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2007a

Community Treasure Hunt

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

Start Hunting!

Translated by