How do I change 1e-5 on a loglog axis out of scientific notation?

I would like to whole y-axis label on my loglog plot to be in standard notation not scientific. For example:
x=loglog([10, 10000], [.00005, .5])
ax=gca
ax.YLim = [0.000001 1];
ax.YTickLabel = ax.YTick;
I've tried a number of methods and looked on a number of forums but no matter what I do the1e-05 and below will not change. Even when you manually change it in the property editor it defaults back to scientific.
Is there a system property that is defaulting this?
The code example above is simplified but illustrates the point.

 Accepted Answer

One possibility:
x=loglog([10, 10000], [.00005, .5]);
grid on
ax=gca;
ax.YLim = [0.000001 1];
ax.YTickLabel = compose('%f', ax.YTick);

7 Comments

Oddly, setting ax.YAxis.Exponent does not have any effect for 'log' scale axes. I didn't know that.

This is closer for sure. Is there a way to kill the trailing zeros using the compose function?

I've been trying to add that for a while but running code in Answers doesn't seem to be working right now.
This code does make it look better (I tried it in my local Matlab).
loglog([10, 10000], [.00005, .5]);
grid on
ax=gca;
ax.YLim = [0.000001 1];
lbls = compose("%f", ax.YTick)
lbls = strip(lbls, "right", "0"); % strip trailing zeros
lbls(end) = lbls(end) + "0"; % add the .0 back onto "1."
ax.YTickLabel = lbls;
I've tried a number of other things like the example below. They all do what is desired except for 1e-05 and below like there is some limit.
ax.YTickLabel = arrayfun(@num2str, ax.YTick, 'UniformOutput', false)
Les Beckham, that is a great solution, thank you. I'm not sure what the issue is and why that is all required but so be it.
I decided to strip the trailing '.' instead of padding a zero so that if the plot goes above 1 it'll be clean.
x=loglog([10, 10000], [.00005, .5])
ax=gca
ax.YLim = [0.000001 10];
y_labels = compose("%f", ax.YTick);
y_labels = strip(y_labels, "right", "0"); % strip trailing zeros
y_labels = strip(y_labels, "right", "."); % strip trailing .
ax.YTickLabel = y_labels;
I'm going to leave this open for a bit to see if anyone knows why this behavior is happening.
It looks like @Thomas and @Les Beckham figured out a solution, but I just wanted to chime in with some additional notes (and confirmations):
  • @Walter Roberson: As you observed, the Exponent has no effect when in log-scale. This is in the documentation, but it is quite easy to miss, as it is strapped onto the end of another sentence at the end of the property description. "If the axis has a log scale, then the Exponent property has no effect."
  • The value of the YTickLabel property is always going to be either a character matrix or a cell-array. If you set the value to be a numeric vector, then the value is immediately converted into a cell-array using an internal algorithm that cannot be customized and is not impacted by any other settings in MATLAB. Ultimately, the internal code is calling the C++ function snprintf.
  • If you want to specify how the numbers are converted into strings, then you need to do the conversion yourself, as you did above using either compose, sprintf, or num2str. Each of those commands have their own unique behaviors. I believe num2str is a very similar algorithm to what is used by the YTickLabel property, but I'm not certain of that.
  • I couldn't find a solution that automatically trimmed the trailing zeros without requiring some manual intervention (such as using strip, as you did above).
One challenge MATLAB faces when automatically trimming trailing zeros is that ultimately none of the numbers in your YTick vector (except 1) are perfectly representable using floating point numbers. You can see this by printing each value to 30 decimal places and noticing that there is some inaccuracy in each case that is inherant to the way MATLAB (and basically every computer) stores numbers.
ticks = logspace(-6,0,7)';
compose('%0.30f',ticks)
ans = 7×1 cell array
{'0.000000999999999999999954748112'} {'0.000009999999999999999123964645'} {'0.000100000000000000004792173602'} {'0.001000000000000000020816681712'} {'0.010000000000000000208166817117'} {'0.100000000000000005551115123126'} {'1.000000000000000000000000000000'}
Unless you specify a specific number of significant digits, MATLAB needs to make a judgement call as to how much precision to show. In some circumstances "0.1" an d"0.1000" are interpretted differently, as the later implies that the value is accurate to 4 decimal places while the former does not.
That being said, it sounds (to me) like a reasonable enhancement to compose to support automatically truncating trailing zeros (and trailing decimal separator). I'll relay that feedback to the appropriate development team.

Sign in to comment.

More Answers (0)

Products

Release

R2024b

Tags

Asked:

on 26 Jun 2025

Commented:

on 30 Jun 2025

Community Treasure Hunt

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

Start Hunting!