Set at least two ticks on log scales in figures

조회 수: 5 (최근 30일)
Isaac Pincus
Isaac Pincus 2023년 1월 24일
댓글: Isaac Pincus 2023년 1월 24일
Often when I plot a figure in log-log scale, I'll get an example like this:
You can see that there is only a single tick label on each axis, which makes it very hard to determine what the actual scale is at a glance. I can of course go in and manually fix it, but if I'm plotting potentially hundreds of similar figures for different sets of data, I'd prefer to have an automatic solution.
Is there any way to get Matlab to automatically use at least 2 tick labels on a plot? Sometimes I can just change the physical size of the window to fix things, other times I can't!
Attached is a minimal working example for the above (formatting might be slightly different as I've changed some other defaults):
figure();
hold on
x = logspace(-2.2,2.2,100);
y = x.^1.2;
plot(x,y);
axes1 = gca;
axes1.XScale = 'log';
axes1.YScale = 'log';
xlabel('$x$', 'Interpreter','latex');
ylabel('$y$', 'Interpreter','latex');

답변 (1개)

Rik
Rik 2023년 1월 24일
I don't know if this is an acceptable solution for you, but you could query the current tick labels and take action in your code:
CurrentTickLabels = axes1.XTickLabels
CurrentTickLabels = 3×1 cell array
{'10^{-2}'} {'10^{0}' } {'10^{2}' }
if numel(CurrentTickLabels)==1
% Do something here to manually set the tick labels
end
  댓글 수: 1
Isaac Pincus
Isaac Pincus 2023년 1월 24일
Thanks, this is helpful! I'll see if I can do anything useful with this. I was hoping that there was some command I was missing to change the default behaviour, but if not I'll have to try this.

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

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by