MATLAB: MATLAB bug for plotting? Overlap between the x-ticks and the number labels: when using inverted y-axis and logarithmic x-axis.

bugplot

Dear Matlab community,
I have been encountering the following issue for some time, and I haven't been able to find a way around it, so I believe it may be a bug.
PROBLEM: overlap between the number labels and the x-axis (and the x-ticks).
See files attached.
This only seem to occur when I use both
  • Inverted y-axis
set(gca, 'YDir','reverse')
  • Logaritmic x-scale
set(gca, 'XScale', 'log')
Any solution? Any suggestion?
Kind regards,
Daniel

Best Answer

  1. The best solution is to upgrade to a newer release of Matlab.
  2. Update your current release might address the problem (I haven't looked into when the x-tick problem was solved).
  3. If updating doesn't fix it and you cannot upgrade, the snippet below will replace your xtick labels with text objects (see inline comments for important details).
Using text to replace axis ticks, robust to axis resizing and axis limit changes
The problem of using text objects to place axis ticks is that text objects are anchored to the axis so if the axis limits change or the figure is resized, the text objects will no longer be in the correct place.
To avoid the problem, add a listener that updates the text objects anytime the axis limit, size, or location changes. Now, when the axis limit changes or the figure resizes, the xtick lable text objects will be reproduced.
% Set up the axes and plot the data
x = logspace(.1,5,10);
y = 1:numel(x);
fig = figure();
ax = axes(fig);
plot(ax,x,y,'-o')
grid on
ax.XScale = 'log';
% Set the listener to respond to changes to the x limits and figure size.
addlistener(ax,'XLim','PostSet',@(ob,ev)updateXTick(ob,ev,ax));
fig.SizeChangedFcn = {@updateXTick,ax};
% Call the listener to create the initial xtick text objects.
updateXTick([],[],ax);
Define the function that responds to figure resize and xlim changes.
function th = updateXTick(~, ~, ax)
% Update pseudo xticklabel text handles.
% ax: axis handle.
% th: text handles to the new xtick labels.
% Set the scaling factor. This is the percentage (in decimal format)
% of the vertical axis space that defines the spacing between the x axis line
% and the text lables. e.g.: 0.1 means 10% of the axis height. 0.0 will place
% the text labels directly under the x axis.
scale = 0;
% Search for and remove the old pseudo xtick labels.
oldXTick = findall(ax,'Tag', 'pseudoXTickLabels');
delete(oldXTick)
% Get the new x-ticks
ax.XTickLabelMode = 'auto';
ax.XTickMode = 'auto';
xt.ticks = ax.XTick;
xt.labs = ax.XTickLabel;
% remove the new xtickslabels
ax.XTickLabel = [];
% Set text objects in place of the xtick labesl
% There may be problems with the yaxis is log scale.
drawnow
yl = ylim(ax);
yLabelPos = yl(1) - (yl(2)-yl(1))*scale;
th = text(ax, xt.ticks, repmat(yLabelPos,size(xt.ticks)), xt.labs,...
'VerticalAlignment', 'top', 'HorizontalAlignment', 'center', ...
'FontSize', ax.FontSize, 'tag', 'pseudoXTickLabels', ... % tag used to ID text obj.
'HandleVisibility', 'off');
end