Force scientific notation in axes
Mostra commenti meno recenti
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
4 Commenti
Daniel Thompson
il 3 Giu 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
il 24 Set 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
Thomas Gillis
il 5 Feb 2019
It should be
e = floor(log10(abs(yl(2))));
instead of
e=log10(yl(2));
e=sign(e)*floor(abs(e));
Risposte (4)
Carla Panarello
il 14 Mag 2024
Modificato: Carla Panarello
il 14 Mag 2024
The question is from many years ago, but maybe this answer can be useful for someone who has the same problem, since the solution is very easy (easier than the code proposed in a previous answer), but it was not easy at all to get it.
So, if you want to force the tick labels to be displayed in exponential notation with the order of magnitude appearing in the top left corner, you can use the following instructions (e.g. for the Y axis):
ax = gca;
ticklabel_exp_notation(ax.YAxis);
where the ticklabel_exp_notation() function can be defined as follows:
function ticklabel_exp_notation(axis)
ord = floor(log10(max(abs(axis.Limits))));
axis.Exponent = ord;
end
NOTE: If the axis limits change, the "order of magnitude" notation remains but the new best suitable order of magnitude is not calculated automatically. So it would be better to apply it at the end, when the figure is complete.
NOTE 2: If left and right axis are present, the function must be applied separately for each axis, e.g. as follows:
ax = gca;
ticklabel_exp_notation(ax.YAxis(1));
ticklabel_exp_notation(ax.YAxis(2));
Steven Lord
il 24 Set 2015
1 voto
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.
Categorie
Scopri di più su Graphics Object Properties in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!