Set at least two ticks on log scales in figures

3 views (last 30 days)
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):
hold on
x = logspace(-2.2,2.2,100);
y = x.^1.2;
axes1 = gca;
axes1.XScale = 'log';
axes1.YScale = 'log';
xlabel('$x$', 'Interpreter','latex');
ylabel('$y$', 'Interpreter','latex');

Answers (1)

Rik on 24 Jan 2023
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
  1 Comment
Isaac Pincus
Isaac Pincus on 24 Jan 2023
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.

Sign in to comment.


Find more on Interactive Control and Callbacks in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!