Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove scientific notation from plot axes

1,193 views
Skip to first unread message

Daniele Bilancione

unread,
Apr 17, 2010, 9:15:22 PM4/17/10
to
Hi everyone!

Is it possible to remove the "x10^y" from the x axis when handling a large variable?

example:

x = zeros(1:220500, 1);
plot(x);
axis([0 length(x) -1 1]);

set(gca, 'XTick', 0:length(x)/10:length(x));

thanks

dpb

unread,
Apr 18, 2010, 1:39:10 PM4/18/10
to

Maybe not optimum, but...

>> xtik=get(gca,'xtick')*1E5;
>> s=sprintf('%d',xtik(1));
>> for i=2:length(xtik)
>> s=strvcat(s,sprintf('%d',xtik(i)));
>> end
>> set(gca,'xticklabel',s)
>>

Didn't explore whether can retrieve the scale factor; if need be can
derive it, of course it was known here.

--


Walter Roberson

unread,
Apr 18, 2010, 2:40:14 PM4/18/10
to
dpb wrote:
> Daniele Bilancione wrote:

>> Is it possible to remove the "x10^y" from the x axis when handling a
>> large variable?

> Maybe not optimum, but...

> >> xtik=get(gca,'xtick')*1E5;
> >> s=sprintf('%d',xtik(1));
> >> for i=2:length(xtik)
> >> s=strvcat(s,sprintf('%d',xtik(i)));
> >> end
> >> set(gca,'xticklabel',s)
> >>

> Didn't explore whether can retrieve the scale factor; if need be can
> derive it, of course it was known here.

The values that are in xtick have not been scaled, and are the full
numeric locations of the tick positions. Rewriting them with scientific
notation is done automatically and only if XTickLabelMode is
'automatic'. As soon as you set an XTickLabel yourself, the scale factor
will be automatically removed.

Thus, the above code should not have the 1E5.

Here is a simpler version of the above code, without any loops:

set(gca, 'XTickLabel', num2str(get(gca, 'XTick')))

If the result doesn't look right, use transpose(get(gca, 'XTick')) [I
can't be bothered at the moment to go in through our VPN to verify
whether the ticks are returned as a row or column vector.]

dpb

unread,
Apr 18, 2010, 2:55:58 PM4/18/10
to
Walter Roberson wrote:
> dpb wrote:
>> Daniele Bilancione wrote:
>
>>> Is it possible to remove the "x10^y" from the x axis when handling a
>>> large variable?
>
>> Maybe not optimum, but...
>
>> >> xtik=get(gca,'xtick')*1E5;
>> >> s=sprintf('%d',xtik(1));
>> >> for i=2:length(xtik)
>> >> s=strvcat(s,sprintf('%d',xtik(i)));
>> >> end
>> >> set(gca,'xticklabel',s)
>> >>
>
>> Didn't explore whether can retrieve the scale factor; if need be can
>> derive it, of course it was known here.
>
> The values that are in xtick have not been scaled, and are the full
> numeric locations of the tick positions. ...

You're right, my bad...I was fiddling and added that after posting when
I shouldn'tve...

Good job on eliminating the loop; didn't think of num2str for some
reason... :( (But, I'll claim since it's beautiful spring Sunday
afternoon, _any_ answer is gravy... :) )


--

Daniele Bilancione

unread,
Apr 18, 2010, 3:39:06 PM4/18/10
to
dpb <no...@non.net> wrote in message <hqfkms$gfm$1...@news.eternal-september.org>...

Thank you guys for the swift replies! I did tried to use num2str as I found a post on the forum but didn't work, but I believe I've figured it out:
being an audio sample I would simply need to scale the sequence by the sampling rate, thus:

timex = (0:(length(x)-1))*(1/fs);
plot(timex, x);
axis([0 length(x)*(1/fs) -1 1]);
grid on;

set(gca, 'XTick', round((0:length(x)/10:length(x))*(1/fs)*100)/100);

I don't know whether it's the optimal route to follow or not but it works! :D

Thanks again!!!

kind regards

Simon

unread,
Feb 16, 2011, 7:47:04 AM2/16/11
to
WARNING!
Hi!

I tried the command:
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))

and I have a warning:
While zooming or panning the xtick numbers change byt xtick labels do not. Be aware!

Steven_Lord

unread,
Feb 16, 2011, 3:16:24 PM2/16/11
to

"Simon " <simon....@epsilon.nu> wrote in message
news:ijgh08$753$1...@fred.mathworks.com...

That's correct -- to have the labels change as well use the
ActionPostCallback of the zoom mode or pan mode object to change them.

http://www.mathworks.com/help/techdoc/ref/zoom.html

http://www.mathworks.com/help/techdoc/ref/pan.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Dan K

unread,
Mar 18, 2013, 4:47:05 PM3/18/13
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <ijhbao$pip$1...@fred.mathworks.com>...
Just thought that I'd add a quick note for those wishing to eliminate scientific notation for the Y axis....

One needs to add an apostrophe after getting the ticks, so it would read like this:
set(gca, 'YTickLabel', num2str(get(gca, 'YTick')'))

-DK

coyote1411

unread,
Feb 12, 2014, 12:25:04 PM2/12/14
to
Just click on the "more properties" button on the axis of the plot and xticklabelmode (or ytick..,ztick..) must be set to "manual" then go to xticklabel and set your labels (should be already there without the scientific notation), click ok and it's done.


Ricardo Arévalo

unread,
Dec 19, 2016, 11:36:08 AM12/19/16
to
"Daniele Bilancione" wrote in message <hqdmfa$43n$1...@fred.mathworks.com>...
Hello Daniele!

If you work with a MATLAB version R2016a you can try the following (I don't know if it applies for previous MATLAB versions):
ax = gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = '%6.0f';

dpb

unread,
Dec 19, 2016, 4:20:29 PM12/19/16
to
On 12/19/2016 10:36 AM, Ricardo Arévalo wrote:
...

> If you work with a MATLAB version R2016a you can try the following (I
> don't know if it applies for previous MATLAB versions):
> ax = gca;
> ax.XAxis.Exponent = 0;
> ax.XAxis.TickLabelFormat = '%6.0f';

Introduced with HG2 in R2013 I believe...not in R2012b for certain.

Bruno Luong

unread,
Dec 19, 2016, 5:04:07 PM12/19/16
to
"dpb" wrote in message <o39is0$21i$2...@dont-email.me>...

>
> Introduced with HG2 in R2013 I believe...not in R2012b for certain.

HG2 i introduced in 2014B

David Willis

unread,
Jan 13, 2017, 2:00:08 PM1/13/17
to
"Bruno Luong" wrote in message <o39lgj$it6$1...@newscl01ah.mathworks.com>...
> "dpb" wrote in message <o39is0$21i$2...@dont-email.me>...
>
> >
> > Introduced with HG2 in R2013 I believe...not in R2012b for certain.
>
> HG2 i introduced in 2014B

This worked great for me and in R2013b the numbers scale when changing the graph window size

plot(data);
yticks = get(gca, 'YTick'); %get the axes
ytickfmt = '%d'; %use whole numbers as default
if max(abs(yticks)) > 1000 %if scale is > 1000, change to K for human readability
ytickfmt = '%dK';
yticks = yticks/1000; %rescale the tick marks
end
set(gca, 'YTickLabel',num2str(yticks', ytickfmt));
0 new messages