Format axes Tick Labels in log scale

조회 수: 250 (최근 30일)
Maurizio De Pitta'
Maurizio De Pitta' 2012년 2월 3일
이동: DGM 2023년 12월 21일
Dear all, an annoying issue when plotting on log scales, is apparently not being able to control the format of axis tick labels.
For example: given XTick = [0.1,1,10,30];
I want to manually set my own labels, e.g. XTickLabels = num2str(10^-1, 10^0, 10^1, 10^-1].
How can I format XTickLabels so that I display tick labels as powers of ten (i.e. base^{exponent} in latex interpreter)?
Right now MATLAB either omits the last label if XTickLabelModel is 'auto' or can display strings of the type '10^X' or can show only numbers as decimals (i.e. '0.1, 1, 10, 0.1').
Is there a way to control directly the format of axis tick labels, or I have to work it around using a text(-) object and switching XTickLabels off?
Thank you in advance for your help. Maurizio
  댓글 수: 1
Amir ghorab
Amir ghorab 2022년 1월 13일
이동: DGM 2023년 12월 21일
hey, I don't know why I can not make this work :((((((((((((((((((((
Can anyone please post a complete example????

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 3일
XTickLabels = cellstr(num2str(round(log10(XTick(:))), '10^%d'));
  댓글 수: 1
Shih-Kai
Shih-Kai 2015년 7월 19일
The solution is awesome. It solves the xticklabels formatted in LaTex interpreter. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Kristian Beedholm
Kristian Beedholm 2014년 10월 23일
But there isn't a latex interpreter for the xticklabels. You have to do it with text, I guess. Solving the same, I came up with this:
y=ylim;
yp=y(1)-abs(diff(ylim))*0.05;
x=get(gca,'xtick');
set(gca,'xticklabel',[],'xtickmode','manual');
for j=1:length(x)
h=text(x(j),yp,['10^{' num2str(log10(x(j))) '}']);
set(h,'HorizontalAlignment','center','fontsize',get(gca,'fontsize'))
end

Cecilia Fleeger
Cecilia Fleeger 2018년 11월 29일
I come across this post and find a solution. To get read of '^' in 10^3, for example is all about TickLabelInterpreter property.
xtick=10.^(-6:-2);
xticklab = cellstr(num2str(round(-log10(xtick(:))), '10^-^%d'));
set(gca,'XTick',xtick,'XTickLabel',xticklab,'TickLabelInterpreter','tex')

Mark Cohen
Mark Cohen 2020년 5월 1일
Here is a simple solution to formatting labels on MATLAB log plots
yt = get(gca,'ytick');
for j=1:length(yt)
% With log plots, MATLAB defaulted to exponential format, that is difficult for lay
% readerst to understand. In my case, I wanted integer format.
YTL{1,j} = num2str(yt(j),'%d');
end
yticklabels(YTL);

Fabian
Fabian 2023년 12월 20일
편집: Fabian 2023년 12월 20일
The easiest what I found is:
set(gca,'Xscale','log')
hope it helps

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by