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

8 visualizaciones (últimos 30 días)
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 comentarios
Daniel Fiuza Dosil
Daniel Fiuza Dosil el 16 de Sept. de 2020
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.
Adam Danz
Adam Danz el 16 de Sept. de 2020
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!

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 16 de Sept. de 2020
Editada: Adam Danz el 16 de Sept. de 2020
  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

Más respuestas (1)

Steven Lord
Steven Lord el 16 de Sept. de 2020
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 comentario
Daniel Fiuza Dosil
Daniel Fiuza Dosil el 16 de Sept. de 2020
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?

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by