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

1 view (last 30 days)
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'')'])

Answers (1)

Shubham
Shubham on 2 Sep 2024
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 Comments
Olaf
Olaf on 6 Sep 2024
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 on 6 Sep 2024
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!

Sign in to comment.

Categories

Find more on 3-D Scene Control in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!