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

7 次查看(过去 30 天)
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 个评论
Daniel Fiuza Dosil
Daniel Fiuza Dosil 2020-9-16
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 2020-9-16
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!

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-9-16
编辑:Adam Danz 2020-9-16
  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

更多回答(1 个)

Steven Lord
Steven Lord 2020-9-16
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 个评论
Daniel Fiuza Dosil
Daniel Fiuza Dosil 2020-9-16
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by