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

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

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

Hello @Voss, thank you for your detailed explanation and taking the problem from a different view point to solve the issue. I am not very good at Matlab but I understand the conflict in here.
In this case, even if I can fix the repeatiton problem, I do not know how to format my label because TickLabelFormat= '%.1e' does not recognized. Is there way to represent my data in e-05 format?
Dear @Voss and @VBBV
I have switched my timetables to arrays and also changes my TickLabelFormat and I have faced with another issue.
Now I have not 2 but 4 rows in my dataset. I attached the dataset. And for some reason the number of the labels are different at first two and last two figures:
My main aim is to put my 4 figures together to a plot in a compact way with x label e-05 format.
Is there any other alternative to receive a similar output? I considered to use subplot but it created similar problems.
%Revised Version - Matlab Q1 for tiledlayout
t = tiledlayout(1,4,'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';
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
%second
nexttile
hAxes = gca;
F19=plot(hAxes,m19(2,:),-z19r1(1:end-1,1)',LineWidth=2);
hold on
F20=plot(hAxes,m20(2,:),-z20_r1(1,1:end-1),LineWidth=2);
hold on
F22=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';
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
%Third
nexttile
hAxes = gca;
J19=plot(hAxes,m19(3,:),-z19r1(1:end-1,1)',LineWidth=2);
hold on
J20=plot(hAxes,m20(3,:),-z20_r1(1,1:end-1),LineWidth=2);
hold on
J22=plot(hAxes,m22(3,:),-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = '%.1e';
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
%Fourth
nexttile
hAxes = gca;
F19=plot(hAxes,m19(4,:),-z19r1(1:end-1,1)',LineWidth=2);
hold on
F20=plot(hAxes,m20(4,:),-z20_r1(1,1:end-1),LineWidth=2);
hold on
F22=plot(hAxes,m22(4,:),-z22(1,1:end-1),LineWidth=2);
hold off
%axes properties
ax = gca;
ax.XAxis.Exponent = 0;
ax.XAxis.TickLabelFormat = '%.1e';
ax.XAxis.FontSize = 20;
ax.YAxis.FontSize = 20;
Output with subplot
If I replace the tiledlayout with subplot, the xlabels are again differently distiributed but this time 3 label points for first two and 2 label points for last two plots.
And I am not sure how can I add a common title in subplot.
I will appreciate your comments to combine my plots.
Thank you!
"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')
Thank you very much for your help @Voss!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Asked:

on 2 Aug 2023

Commented:

on 3 Aug 2023

Community Treasure Hunt

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

Start Hunting!