Force scientific notation in axes

pfb (view profile)

on 15 Oct 2014
Latest activity Answered by Steven Lord

Steven Lord (view profile)

on 24 Sep 2015
Sometimes, when the "raw" values in the yticks are very small, matlab y axis automatically toggles to scientific notation, whereby the power of ten giving the order of magnitude appears in the top left corner, and the yticks are given in units of that power. The threshold for this behavior seems to be 1e-3, but I can't seem to find a property for forcing it on larger yticks.
I have found a few questions roughly on the same topic, but none of the relevant answers seem to apply to my case. Some people simply wanted to get rid of the scientific notation, other wanted it directly in the tick labels.
I like the "order of magnitude" format, but I am unable to force it (for instance, I'd like to have it for yticks of the order of 1e-2, for graphical homogeneity with a different plot).
Before downloading or creating an "ad hoc" code, I wanted to ask whether any of you knows a (perhaps undocumented?) way of toggling the "order of magnitude" notation.
Thanks a lot
Francesco

pfb

pfb (view profile)

on 15 Oct 2014
well, getting what I need does not require a lot of coding. For anyone interested, here is a quick example which does the trick when the focus is on the figure to be modified.
As soon as I have some time I'll turn it into a more complete function (unless something like this already exists).
% get ylim
yl=ylim;
% get order of magnitude
e=log10(yl(2));
e=sign(e)*floor(abs(e));
% get and rescale yticks
yt=get(gca,'ytick')/10^e;
% create tick labels
ytl=cell(size(yt));
for j=1:length(yt)
% the space after the percent gives the same size to positive and
% negative numbers. The number of decimal digits can be changed.
ytl{j}=sprintf('% 1.2f',yt(j));
end
% set tick labels
set(gca,'yticklabel',ytl);
% place order of magnitude
fs = get(gca,'fontsize');
set(gca,'units','normalized');
xl = xlim;
text(xl(1),yl(2),sprintf('x 10^%d',e),...
'fontsize',fs,'VerticalAlignment','bottom');
Daniel Thompson

Daniel Thompson (view profile)

on 3 Jun 2015
Exactly what I've just spent half an hour looking for - thanks so much for this!
One very small edit: change the penultimate line of code:
text(xl(1),yl(2),sprintf('x 10^%d',e),...
to
text(xl(1),yl(2),sprintf('\\times10^%d',e),...
for a real multiplication sign (as opposed to the letter 'x').
(I only spotted this because I have multiple subplots, some with the automatic scientific notation - and the multiplication sign - and one which I am forcing using your code.)
Dinant Kistemaker

Dinant Kistemaker (view profile)

on 24 Sep 2015
one more very small change of:
text(xl(1),yl(2),sprintf('\\times10^%d',e),...
into:
text(xl(1),yl(2),sprintf('\\times10^{%d}',e),...
to correctly show the superscript

Steven Lord (view profile)

on 24 Sep 2015

Take a look at the new axis customization functionality introduced in release R2015b and described in this post on Loren's blog. You may find some of the techniques described in that post useful.

on 15 Oct 2014

pfb

pfb (view profile)

on 15 Oct 2014
Hi David,
thanks a lot for your prompt reply. I missed the stack overflow very neatly posed question, but stumbled upon the one in matlab answers it points to. Ccook's "no-go" reply was conditioned to setting YTickLabel ("If you set YTickLabel, then there is no (documented) way to get MATLAB to automatically put in the exponent the same way.") I think that's pretty obvious... How can matlab know about the order of magnitude if you set the tick labels, which are strings? Perhaps he meant "YTick". By the way that's what prompted me to ask about "possibly undocumented" ways to do that.
It's a bit frustrating that there is no toggle for this behavior. After all, somewhere in the code for the figure there must be some check on the order of magnitude for the plot range. It seems to me that changing the default threshold for that check would do the trick.
I am not even able to get a handle for the text giving the order of magnitude, so it must be something in the axes structure (as opposed to a simple bit of text).
1) my statement of the problem is not very precise. Of course the order of magnitude behavior does not apply only to "very small" plot ranges, but also to "very large" ones. It seems to me that the lower and upper bounds are 1e-3 and 1e+4.
2) I have checked that changing the ylim (which sets YLimMode to "manual") does not remove the scientific notation, provided that the new range is compatible with the above thresholds.

on 15 Oct 2014
Edited by Iain

Iain (view profile)

on 15 Oct 2014

Here's a really simple option.
Change the units of the axes. Plot time, in, for example, hs, cs, ds, Ds, ms, ks ...

pfb

pfb (view profile)

on 15 Oct 2014
Hi Iain,
thanks for your contribution. However, this is not what I would like to get.
As I say, I like the order of magnitude thing. It is standard, compact, homogeneous (two plots with different orders of magnitude would "look" the same, except for the exponent).
Your method requires specifying the units... This can be done in the figure caption or perhaps in the axes labels. Either way, it is less compact, and I really have tight spatial constraints.
Iain

Iain (view profile)

on 15 Oct 2014
You do realise that people will ask "So what are the units?" if you don't put them on the labels...
pfb

on 18 Apr 2015
Hi lain,