- What matlab release are you using?
- Can you provide a minimal working example so we can reproduce this on our end?
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 个评论
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
2020-9-16
编辑:Adam Danz
2020-9-16
- The best solution is to upgrade to a newer release of Matlab.
- Update your current release might address the problem (I haven't looked into when the x-tick problem was solved).
- 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
0 个评论
更多回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!