force a scientific notation format
조회 수: 47 (최근 30일)
이전 댓글 표시
hi,
when i use this code, instead of forcing it to give me the x-axis as: 0,1,2,...,10 then x10^3 (3 is a superscript) it gives me the numbers as 1000,2000,...and so on...any idea?
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
set(gca,'xtick',0:0.1e4:0.1e5);
댓글 수: 4
José-Luis
2012년 12월 25일
편집: José-Luis
2012년 12월 25일
As a general rule, people will be more inclined to answer a well posed, clear question. When I see unedited text posed as a question I often tend to ignore it, unless it is very short (like yours was). Other contributors might be more dedicated, but I think a nice presentation increases your chances for an answer.
neo
2012년 12월 25일
편집: neo
2012년 12월 25일
I think following link may help:
I also feel this is a problem. The obvious solution would have been to edit XTickLabel using latex/tex interpreter. But Matlab help clearly states that one can not use latex/tex for axis tick labels. So solutions like above replaces them by text objects and then use latex/tex interpreter.
I Wonder how matlab does it for log-log or semilog plots or when axis limit exceeds about 10^5 or similar cases.
채택된 답변
José-Luis
2012년 12월 25일
편집: José-Luis
2012년 12월 25일
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
xVals = 0:0.1e4:0.1e5;
set(gca,'xtick',xVals);
set(gca,'XTickLabel',sprintf('%2.0e|',xVals));
The last line in the previous snippet sets the format for your XTickLabels. For more information on other formats:
doc sprintf
EDIT Exponent should appear at the right of the X-axis
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
xVals = 0:0.1e4:0.1e5;
set(gca,'xtick',xVals);
expVal = 3; %exponent you want
set(gca,'XTickLabel',sprintf('%2.0f|',xVals./(10^expVal)));
pos = get(gca,'Position');
offset = 0.00; %how far to the right you want the exponent
annotation('textbox',[pos(1)+ pos(3)+offset, pos(2), 0.2, 0.2],...
'String',['$\times 10^' num2str(expVal) '$'],...
'Interpreter','latex',...
'VerticalAlignment','bottom',...
'EdgeColor','none')
댓글 수: 4
추가 답변 (1개)
Jonathan Epperl
2012년 12월 25일
Afaik that's not possible, since the tick labels aren't like normal text objects in that they support a subset of latex commands. I recommend tick2text ( http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels ) from the FEX. Once you have it on your Matlab path:
x = 10.^(1:10);
plot(x,x)
g = gca;
tick2text(g,'axis','x')
h = getappdata(g,'XTickText')
for i=1:numel(h), set(h(i),'String',num2str(i,'10^%d')); end
댓글 수: 1
José-Luis
2012년 12월 25일
What do you mean it's not possible? You can set XTickLabel to whatever string you want, even if it has nothing to do with the numeric value. You can't use Latex as the interpreter though, I'll agree to that, but that's not relevant for what the op asked, if I understood correctly.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!