Hello,
Just a quick question, I intended to compare numerical solutions and analytical solutions of a system. I plot them. But, the y-axis range is suspicious. Please see below plot.
Do you have any idea why Matlab doing this. I guess, the difference between two axis ticks is smaller than 10^(-4), that is why it did not include those 10^(-4) part. Am I right?

1 commentaire

Okay I found an answer. Basically, I need to add
set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%d'))
But now, my y-axis ticks look like
But I don't like this appearance. Is it possible to use
10^(-1)
notation or
decimal notation
instead of
e-01?
Also, how can I get rid of
two zeros
before
e
terms in y-axis ticks..

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 12 Oct 2016

0 votes

It probably is. Instead of num2str, use sprintf.
Experiment with the format descriptor string to get the result you want.

9 commentaires

Meva
Meva le 12 Oct 2016
Modifié(e) : Meva le 12 Oct 2016
Thanks Star Strider, but I have
Error using sprintf
Invalid format.
when I try sprintf.
I used
set(gca, 'YTickLabel', sprintf(get(gca,'YTick')','%d'))
I’d forgotten that sprintf works differently than num2str. It’s probably easier to go back to the way you had it, and change the format descriptor to '%.5f':
set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%.5f'))
That will work.
To get the exponent formats as ‘10²’ and such would require sprintf and more code to impelement it. If you’re happy using the set call here, I would go with it.
Meva
Meva le 12 Oct 2016
Modifié(e) : Meva le 12 Oct 2016
Thanks so much Star Strider. This is fantastic. Also, just for the possible future usage, could you please inform me about how to get the axis labels in the exponent formats as 10^2 ?
My pleasure.
There are two ways to get the exponent form at as ‘10²’. The easiest is to set the y-scale to 'log'. If you want to maintain a linear scale, this works:
x = linspace(1, 5, 10); % Create Data
y = 10.^x; % Create Data
figure(1)
plot(x, y, '-p')
grid
hAx = gca; % Get Axis Handle
% hAx.YScale = 'log'; % Set Y-Scale To Logarithmic
ytix = hAx.YTick; % Get Y-Tick Values
ytix_exp = fix(log10(ytix)); % Y-Tick Label Exponents
ytix_exp(~isfinite(ytix_exp)) = 0; % Set ‘-Inf’ To ‘0’
ytix_mnt = 10.^rem(log10(ytix),1); % Y-Tick Label Mantissas
ytix_mnt(~isfinite(ytix_mnt)) = 0; % Set ‘NaN’ To ‘0’
ytkstr = sprintf('%.5f 10^{%d}\n', [ytix_mnt; ytix_exp]); % Create Strings For Y-Labels, Choose Mantissa Format As: '%.5f'
ytix_lbl = regexp(ytkstr, '\n', 'split'); % Separate Strings To Create Y-Tick Labels
set(hAx, 'YTick',ytix', 'YTickLabel',ytix_lbl) % Set Y-Tick Labels
The sprintf call controls the format. The first ‘%.5f’ creates numbers with 5 decimal places, since in this example you want that precision. See the documentation for sprintf for details.
There are several ways to handle zero. I took the easy way out here and created it as ‘0°’. Another way is to simply set it to ‘0’ if you know where it is.
The complete code for that is:
x = linspace(1, 5, 10); % Create Data
y = 10.^x; % Create Data
figure(1)
plot(x, y, '-p')
grid
hAx = gca; % Get Axis Handle
% hAx.YScale = 'log'; % Set Y-Scale To Logarithmic
ytix = hAx.YTick; % Get Y-Tick Values
ytix_exp = fix(log10(ytix)); % Y-Tick Label Exponents
ytix_exp(~isfinite(ytix_exp)) = 0; % Set ‘-Inf’ To ‘0’
ytix_mnt = 10.^rem(log10(ytix),1); % Y-Tick Label Mantissas
ytix_mnt(~isfinite(ytix_mnt)) = 0; % Set ‘NaN’ To ‘0’
ytkstr = sprintf('%.5f 10^{%d}\n', [ytix_mnt; ytix_exp]); % Create Strings For Y-Labels, Choose Mantissa Format As: '%.5f'
ytix_lbl = regexp(ytkstr, '\n', 'split'); % Separate Strings To Create Y-Tick Labels
ytix_lbl{1} = '0'; % Set First Y-Yick Label To '0'
set(hAx, 'YTick',ytix', 'YTickLabel',ytix_lbl) % Set Y-Tick Labels
The problem arises if the zero comes somewhere in the middle of the labels. If it exactly zero, testing for non-finite values in the same place in both the mantissa and exponent vectors would determine the zero, but if it is not exactly zero, you would need to write the appropriate logic to test for it.
There are probably different ways to do this, some more efficient. I opted for more transparent code rather than more efficient code so you can see how it works and can experiment with it.
Meva
Meva le 13 Oct 2016
This is perfect! This is exactly what I want. Many thanks Star Strider.
Meva
Meva le 13 Oct 2016
Modifié(e) : Meva le 13 Oct 2016
In my problem, my y-axis labels like
0.240025 10^0
Is there any way to remove this redundant
10^0
part? I tried change the respective line with
ytkstr = sprintf('%.5f');
However, it did not go well.
PS: I used your last recommendation code for this.
If none of your tick labels have exponents, one quick fix is simply to replace the ‘ytixstr’ assignment with:
ytkstr = sprintf('%.5f\n', ytix_mnt); % Create Strings For Y-Labels, Choose Mantissa Format As: '%.5f'
If some do and others don’t, the code quickly gets very complicated. It’s not something I want to write just now.
Meva
Meva le 16 Oct 2016
I do appreciate your nice tips @Star Strider. This works.
Star Strider
Star Strider le 16 Oct 2016
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by