Weird plot axis range and ticks
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
Meva
2016 年 10 月 12 日
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..
採用された回答
Star Strider
2016 年 10 月 12 日
It probably is. Instead of num2str, use sprintf.
Experiment with the format descriptor string to get the result you want.
9 件のコメント
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'))
Star Strider
2016 年 10 月 12 日
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.
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 ?
Star Strider
2016 年 10 月 13 日
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
2016 年 10 月 13 日
This is perfect! This is exactly what I want. Many thanks Star Strider.
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.
Star Strider
2016 年 10 月 13 日
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
2016 年 10 月 16 日
I do appreciate your nice tips @Star Strider. This works.
Star Strider
2016 年 10 月 16 日
As always, my pleasure.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Axis Labels についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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
- United Kingdom (English)
