minor grid line spacing on log axes of less than 1 decade (answered by myself)

78 vues (au cours des 30 derniers jours)
rob06
rob06 le 25 Nov 2015
Modifié(e) : rob06 le 30 Nov 2015
Apologies for suggesting Matlab might be wrong - I've figured it out...
When a log axis is less than 1 decade Matlab takes the previous minor gridlines and makes them into major gridlines, and adds new fractional minor gridlines (forgive my clunky way of describing it). In doing this, the axis can appear to become linear rather than log, and this can make log graphs look a bit odd (IMHO)
Here is an example which demonstrates the problem...
x = 1:0.025:2; y = zeros(size(x)); plot(x,y,'.'); set(gca,'Xscale','log'); grid on
Now study the minor gridlines - each 'decade' has 5 minor grid ticks, which may or may not line up with the plotted dots, depending on the physical size of your graph.
Change the x to 1:0.025:10 and the graph looks completely different. From now on I am going to use a xlim([1 10]) to prevent this.
In general, HG2 is a big improvement though

Réponses (1)

Mike Garrity
Mike Garrity le 25 Nov 2015
They're not really log spaced differently.
Let's look at an example:
ydat = linspace(1,100,500);
semilogy(ydat)
ax = gca;
ax.YGrid = 'on';
ax.YMinorGrid = 'on';
What's happening is that they split each decade evenly, but the magnitude of the decade is changing:
for y=1:10
t = text(200,y,sprintf('%d',y));
t.FontSize = 6;
t.HorizontalAlignment = 'center';
t.VerticalAlignment = 'baseline';
end
for y=20:10:100
t = text(200,y,sprintf('%d',y));
t.FontSize = 6;
t.HorizontalAlignment = 'center';
t.VerticalAlignment = 'baseline';
end
That's MATLAB's default for log tick spacing, and it seems to be the most reasonable one for most situations. Especially when you have data which spans many decades.
However, it's not always what you want. Starting in R2015b, you can override that default and use whatever you'd like for the minor tick spacing.
You'd do that like this:
ax.YAxis.MinorTickValues = 1:100;
You can even use this to place the ticks at values that will be evenly spaced on the screen by giving them values which are scaled exponentially.
semilogy(ydat)
ax = gca;
ax.YGrid = 'on';
ax.YMinorGrid = 'on';
ax.YAxis.MinorTickValues = 10.^linspace(0,2,25);
for y=ax.YAxis.MinorTickValues
t = text(200,y,sprintf('%g',y));
t.FontSize = 6;
t.HorizontalAlignment = 'center';
t.VerticalAlignment = 'baseline';
end
Is one of those what you're looking for?

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by