Force scientific notation in axes (2024)

64 views (last 30 days)

Show older comments

pfb on 15 Oct 2014

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes

Edited: Carla Panarello on 14 May 2024

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 hom*ogeneity 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 Comments

Show 2 older commentsHide 2 older comments

pfb on 15 Oct 2014

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243190

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243190

Edited: pfb on 15 Oct 2014

Open in MATLAB Online

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 on 3 Jun 2015

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_289731

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_289731

Open in MATLAB Online

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 on 24 Sep 2015

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_311955

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_311955

Open in MATLAB Online

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 on 5 Feb 2019

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_668067

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_668067

Open in MATLAB Online

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

instead of

e=log10(yl(2));

e=sign(e)*floor(abs(e));

Sign in to comment.

Sign in to answer this question.

Answers (4)

Steven Lord on 24 Sep 2015

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_193597

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_193597

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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

David Sanchez on 15 Oct 2014

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_155231

1 Comment

Show -1 older commentsHide -1 older comments

pfb on 15 Oct 2014

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243178

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243178

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

A couple of comments

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.

Sign in to comment.

Iain on 15 Oct 2014

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_155236

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_155236

Edited: Iain 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 ...

3 Comments

Show 1 older commentHide 1 older comment

pfb on 15 Oct 2014

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243183

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243183

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, hom*ogeneous (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 on 15 Oct 2014

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243243

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_243243

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

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_279426

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#comment_279426

Hi lain,

I noticed your comment just now. Sorry for the late reply. I'm replying anyway because your comment seems unnecessarily contentious.

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"

Sign in to comment.

Carla Panarello on 14 May 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_1457031

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/158707-force-scientific-notation-in-axes#answer_1457031

Edited: Carla Panarello on 14 May 2024

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels

Find more on Axis Labels in Help Center and File Exchange

Tags

  • axes
  • ticks
  • scientific notation

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Force scientific notation in axes (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Force scientific notation in axes (2024)

References

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6101

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.