MATLAB: Possible bug in plotting with log-scale? Inconsistent log-scales in tiled plots

figureloglogplotMATLABplotsubplotyscale

I recently stumbled upon some weird behaviour when trying to generate several subplots with a logarithmic y-scale. The subplots are constructed in a loop, so that each subplot is generated exactly the same way, still the Y-scale differs between the various subplots even though they all have the same limits and tick definition, which does not make any sense to me. How can I make sure that the Y-scale and ticks are equal in all subplots? Below is an example that illustrates the problem (Matlab R2019a).
figure;
for i=1:9
ax = subplot(3,3,i);
plot(rand(5));
yticks([0:0.2:1]);
ylim([0, 1]);
set(ax, "YScale", "log");
end
Output:
The effect is especially clear on the third row of subplots where the y-ticks are shifted upwards and compressed relative to the other subplots.

Best Answer

Apparently this behavior stems from setting the lower limit in ylim() to zero, which is not possible on a logarithmic scale. Thus causing Matlab to automatically define the lower ylimit to the lowest datapoint to be plotted. Setting
ylim([1e3, 1]);
solves the issue.