How can I put XTickLabels properly when using YDir = reverse and XScale = log?

조회 수: 1 (최근 30일)
Olaf
Olaf 2019년 1월 17일
댓글: Olaf 2024년 9월 6일
The XTickLabels are not put properly on the graph when I do:
set(gca,'XScale','log','YDir','reverse')
I would like to have the XTicklabels on the right place without overlapping the ticks.
x = [1e-5:0.5:100];
y = x;
subplot(3,1,1)
plot(x,y)
set(gca,'XScale','log','YDir','reverse','TickDir','in')
title(['set(gca,''XScale'',''log'',''YDir'',''reverse'',''TickDir'',''in'')'])
set(gca,'fontsize',14)
subplot(3,1,2)
plot(x,y)
set(gca,'XScale','log','YDir','reverse','TickDir','out')
title(['set(gca,''XScale'',''log'',''YDir'',''reverse'',''TickDir'',''out'')'])
set(gca,'fontsize',14)
subplot(3,1,3)
plot(x,y)
set(gca,'YDir','reverse','TickDir','in')
title(['set(gca,''YDir'',''reverse'',''TickDir'',''in'')'])

답변 (1개)

Shubham
Shubham 2024년 9월 2일
Hi Olaf,
When using logarithmic scales and reversing the Y-axis, MATLAB's automatic tick placement can sometimes lead to overlapping ticks or misaligned labels. Here are some strategies to ensure your XTickLabels are placed correctly without overlapping the ticks:
Approach
  1. Manual Tick Placement: Specify the X-ticks manually to ensure they are placed at appropriate positions.
  2. Adjust XTickLabel Position: Use the XTickLabel property to manually set the labels if necessary.
  3. Increase Figure Size: Sometimes increasing the figure size can help in reducing overlap.
Here's how you can implement these strategies in your code:
x = [1e-5:0.5:100];
y = x;
% Create the first subplot
subplot(3,1,1);
plot(x, y);
set(gca, 'XScale', 'log', 'YDir', 'reverse', 'TickDir', 'in', 'fontsize', 14);
title("set(gca,'XScale','log','YDir','reverse','TickDir','in')");
% Manually setting X-ticks
xticks([1e-5, 1e-3, 1e-1, 1, 10, 100]);
xticklabels({'10^{-5}', '10^{-3}', '10^{-1}', '1', '10', '100'});
% Create the second subplot
subplot(3,1,2);
plot(x, y);
set(gca, 'XScale', 'log', 'YDir', 'reverse', 'TickDir', 'out', 'fontsize', 14);
title("set(gca,'XScale','log','YDir','reverse','TickDir','out')");
% Manually setting X-ticks
xticks([1e-5, 1e-3, 1e-1, 1, 10, 100]);
xticklabels({'10^{-5}', '10^{-3}', '10^{-1}', '1', '10', '100'});
% Create the third subplot
subplot(3,1,3);
plot(x, y);
set(gca, 'YDir', 'reverse', 'TickDir', 'in', 'fontsize', 14);
title("set(gca,'YDir','reverse','TickDir','in')");
% Manually setting X-ticks
xticks([1e-5, 1e-3, 1e-1, 1, 10, 100]);
xticklabels({'10^{-5}', '10^{-3}', '10^{-1}', '1', '10', '100'});
Explanation
  • Manual X-Ticks: By using xticks and xticklabels, you have precise control over where the ticks and labels are placed. This helps prevent overlapping and ensures clarity.
  • Logarithmic Labels: Using LaTeX-style labels (e.g., '10^{-5}') can make the tick labels more readable and consistent with logarithmic scales.
  • Consistency Across Subplots: Ensure that each subplot has the same tick settings for consistency.
By following these steps, you should be able to achieve a clear and well-aligned plot with logarithmic scales and reversed axes.
  댓글 수: 2
Olaf
Olaf 2024년 9월 6일
Hi Shubham,
thanks for your suggestion!
I saw that the problem was fixed meanwhile by Matlab (the problem was reported 5 years ago). So if I run the original code today, there is no problem (see attached PDF).
Olaf
Olaf 2024년 9월 6일
Hi Shubham,
running your code, there is a problem in subplot 3 with the labels (see attached PDF).
Again, thanks a lot for your comment!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by