Weird plot axis range and ticks
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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일
0 개 추천
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'))
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 ?
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.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
