Clear Filters
Clear Filters

x-axis values repeat themselves when I use tiledloyout for plots

9 views (last 30 days)
Hello,
I am using tiledlayout to combine my plots. However the values on the x axes are somehow repeating itself e.g. 0 1 2 3 4 0 1 2 3 4 when I used tiledlayout.
If you plot individually, the values are presented correctly:
%INDIVIDUAL PLOT
J19=plot(m19{1,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(m20{1,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(m22{1,:},-z22(1,1:end-1),LineWidth=2);
%hold off
%axes properties
ax = gca;
xtick_values = ax.XTick;
xtick_labels = compose('%.1e', xtick_values);
ax.XTickLabel = xtick_labels;
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
If you use tiledlayout, xaxes values are presented wrong:
%Tiledlayout
t = tiledlayout(1,2,'TileSpacing','Compact');
%first
nexttile
hAxes = gca;
J19=plot(hAxes,m19{1,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20{1,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22{1,:},-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
xtick_values = ax.XTick;
xtick_labels = compose('%.1e', xtick_values);
ax.XTickLabel = xtick_labels;
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
%second
nexttile
hAxes = gca;
J19=plot(hAxes,m19{2,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20{2,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22{2,:},-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
xtick_values = ax.XTick;
xtick_labels = compose('%.1e', xtick_values);
ax.XTickLabel = xtick_labels;
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
What might be the reason of the problem and how can I fix this issue?
Thank you in advance!

Accepted Answer

Voss
Voss on 2 Aug 2023
Edited: Voss on 2 Aug 2023
The reason the xticklabels repeat is that there are fewer labels than there are ticks. Now, you may say, "How can that be? I set the labels based on the ticks." That's true, so initially at the time you set the labels, they should not repeat because the number of ticks and labels is the same, but after that certain events can happen - such as the figure resizing or the user zooming in or panning - which cause the xticks to change. If that event causes more xticks to be added, then you'll see the repeating xticklabels behavior.
It's a situation where the xticklabels are fixed (because you specified them), but the xticks might change (because you didn't specify them). For example, if the xticklabels have been specified as {'1', '2', '3'} because the xticks were [1, 2, 3] initially, but then the xticks become [1, 1.5, 2, 2.5, 3], then MATLAB assigns the labels in order of the ticks, repeating if necessary, so what you get is {'1', '2', '3', '1', '2'}.
More concretely in terms of axes properties, the situation is that the XTickLabelMode is 'manual' (because you specified the xticklabels) but the XTickMode is 'auto' (because you never specified the xticks).
load Qdata.mat
%Tiledlayout
t = tiledlayout(1,2,'TileSpacing','Compact');
%first
nexttile
hAxes = gca;
J19=plot(hAxes,m19{1,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20{1,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22{1,:},-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
xtick_values = ax.XTick;
xtick_labels = compose('%.1e', xtick_values);
ax.XTickLabel = xtick_labels;
And we can confirm that that is the case here:
ax.XTickLabelMode
ans = 'manual'
ax.XTickMode
ans = 'auto'
Now, setting XTickMode to 'manual' would fix the problem, but that would also make it so that the XTicks never change when you resize the figure, zoom/pan, etc. What you really are after, it looks like, is setting the ax.XAxis.TickLabelFormat and ax.XAxis.Exponent, rather than trying to explicitly set the XTickLabels.
Try something like this:
figure
%Tiledlayout
t = tiledlayout(1,2,'TileSpacing','Compact');
%first
nexttile
hAxes = gca;
J19=plot(hAxes,m19{1,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20{1,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22{1,:},-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = '%.1e';
%second
nexttile
hAxes = gca;
J19=plot(hAxes,m19{2,:},-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20{2,:},-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22{2,:},-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = '%.1e';
  4 Comments
Voss
Voss on 3 Aug 2023
"for some reason the number of the labels are different at first two and last two figures"
The XLim are also different; that's what causes the xticklabels to be different. You can force the XLim to be the same across all axes using linkaxes. See the code below, for example.
I don't know of a good way to get the xticklabels to be of the form "e-05" as opposed to "x10^-5". One thing you can try is to multiply the x-coordinates of the stuff you plot by 1e5, so that the XData and XTicks are 1e5 times as big, and then set the XAxis.TickLabelFormat to '%.1fe-05', as below.
(The code from your last comment didn't match the mat-file uploaded there nor the original mat-file, so I'm using the code from my answer and the original mat-file.)
figure
load Qdata.mat
%Tiledlayout
t = tiledlayout(1,2,'TileSpacing','Compact');
%first
nexttile
J19=plot(m19{1,:}*1e5,-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(m20{1,:}*1e5,-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(m22{1,:}*1e5,-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
hAxes = gca;
hAxes.XAxis.Exponent = 0;
hAxes.XAxis.TickLabelFormat = '%.1fe-05';
%second
nexttile
J19=plot(m19{2,:}*1e5,-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(m20{2,:}*1e5,-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(m22{2,:}*1e5,-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
hAxes(2) = gca;
hAxes(2).XAxis.Exponent = 0;
hAxes(2).XAxis.TickLabelFormat = '%.1fe-05';
linkaxes(hAxes,'x')

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!