Set at least two ticks on log scales in figures

5 vues (au cours des 30 derniers jours)
Isaac Pincus
Isaac Pincus le 24 Jan 2023
Commenté : Isaac Pincus le 24 Jan 2023
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');

Réponses (1)

Rik
Rik le 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
end
  1 commentaire
Isaac Pincus
Isaac Pincus le 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.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by