How can I put XTickLabels properly when using YDir = reverse and XScale = log?
Show older comments
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
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
- Manual Tick Placement: Specify the X-ticks manually to ensure they are placed at appropriate positions.
- Adjust XTickLabel Position: Use the XTickLabel property to manually set the labels if necessary.
- 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.
Categories
Find more on Axes Appearance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!