# Force scientific notation in axes

70 ビュー (過去 30 日間)
pfb 2014 年 10 月 15 日

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 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Dinant Kistemaker 2015 年 9 月 24 日
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 2019 年 2 月 5 日
The answer of phb worked for me, except that there is a small problem in the exponent computation.
It should be
e = floor(log10(abs(yl(2))));
e=log10(yl(2));
e=sign(e)*floor(abs(e));

サインインしてコメントする。

### 回答 (4 件)

Steven Lord 2015 年 9 月 24 日
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.
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

サインインしてコメントする。

David Sanchez 2014 年 10 月 15 日
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
pfb 2014 年 10 月 15 日
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.

サインインしてコメントする。

Iain 2014 年 10 月 15 日

Here's a really simple option.
Change the units of the axes. Plot time, in, for example, hs, cs, ds, Ds, ms, ks ...
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Iain 2014 年 10 月 15 日
You do realise that people will ask "So what are the units?" if you don't put them on the labels...
pfb 2015 年 4 月 18 日
Hi lain,
Have you read my comment? Let me quote myself
"Your method requires specifying the units... This can be done in the figure caption or perhaps in the axes labels"
If someone asked "So what are the units?" I could reply "Pay attention! Look at the axes label and the figure caption"

サインインしてコメントする。

Carla Panarello 2024 年 5 月 14 日

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));

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by