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

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

7 Commenti

  1. What matlab release are you using?
  2. Can you provide a minimal working example so we can reproduce this on our end?
Flipping the ydir and setting the x scale to log does not cause a problem for me (r2019b)
h = axes();
h.YDir = 'reverse';
h.XScale = 'log';
% Everything looks fine in r2019b
If I remember ccorrectly, that has since been fixed. Be certain you have all the latest Updates for your MATLAB version.
FYI, and following my previous response:
Your code, in my release of Matlab 2018b, reproduce the error:
Unfortunately, I don't want to update my MATLAB version at the moment. I had problems in the past with programs not working with updated versions of Matlab, and a very frustrating experience debugging them and correcting them...
  1. Any possible solution around this very simple problem?
  2. If no, is it possible to install two versions of MATLAB simultaneously?
Yes, you can install multiple versions of Matlab.
I find it surprising that programs didn't work in later releases of Matlab. With few exceptions, matlab releases are backward compatible so upgrading shouldn't be a problem.
If you don't want to use a new release, make sure you've installed all of the updates for your current release.
An alternative to upgrading to a newer release is to set the tick labels manually with text objects - this often causes a different set of problems with the axis resizes.
Thanks Adam for all your suggestions.
I didn't know you can update a certain release without upgrading the version! (I will check now how to do that)
Do you have any example on how to set the tick labels manually? Because I would rather find a workaround solution than to upgrade my Matlab version.
Daniel Fiuza Dosil's answer moved here as a comment.
Please find below a minimal working example that reproduces the error in Matlab 2018b:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
set(gca,'YDir','reverse')
set(gca,'XScale','log')
See below:
Thanks for your help!

Accedi per commentare.

 Risposta accettata

  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

Più risposte (1)

When I run the code in the Description section of bug report 1832634 I see the same type of problem as in your reproduction steps. That bug is listed as fixed in release R2019a.

1 Commento

I was not familiar with the 'bug report' section in Mathworks. Thanks for poiting that out Steven!
So I imagine the only possible option now is to upgrade my version to R2019a. I just updated my 2018b version, and that didn't work. Maybe there is a way of manually moving and positioning the number label in the x-axis some distance below?

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by