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

2 Ansichten (letzte 30 Tage)
Olaf
Olaf am 17 Jan. 2019
Kommentiert: Olaf am 6 Sep. 2024
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'')'])

Antworten (1)

Shubham
Shubham am 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 Kommentare
Olaf
Olaf am 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 am 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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 3-D Scene Control finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by