Fix Text Overflowing the Axes When Plotting Figures in MATLAB

Oct. 20, 2025 • Updated Oct. 19, 2025

When we plot figures in MATLAB, sometimes x-label (maybe some other text) would overflow the axes, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
clc, clear, close all

X = categorical({'Small', 'Medium', 'Large', 'Extra Large'});
X = reordercats(X, {'Small', 'Medium', 'Large', 'Extra Large'});
Y = [10, 21, 33, 52];

figure("Color", "w")
tiledlayout(1, 2)

nexttile
bar(X,Y)
xlabel("Categories", "FontName", "Times New Roman", "FontSize", 30)
title("Bar plot", "FontName", "Times New Roman", "FontSize", 30)

nexttile
bar(X,Y)
xlabel("Categories", "FontName", "Times New Roman", "FontSize", 30)
title("Bar plot", "FontName", "Times New Roman", "FontSize", 30)

exportgraphics(gcf, "fig.jpg", "Resolution", 600)

fig

I guess it is caused by the tiledlayout function. To solve this problem, we can increase the height of figure and then move the whole tiled chart layout upward:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
clc, clear, close all

X = categorical({'Small', 'Medium', 'Large', 'Extra Large'});
X = reordercats(X, {'Small', 'Medium', 'Large', 'Extra Large'});
Y = [10, 21, 33, 52];

fig = figure("Color", "w");
t = tiledlayout(1, 2);

nexttile
bar(X,Y)
xlabel("Categories", "FontName", "Times New Roman", "FontSize", 30)
title("Bar plot", "FontName", "Times New Roman", "FontSize", 30)

nexttile
bar(X,Y)
xlabel("Categories", "FontName", "Times New Roman", "FontSize", 30)
title("Bar plot", "FontName", "Times New Roman", "FontSize", 30)

% IMPORTANT to adopt the same position units for fig and tiled chart layout
fig.Units  = "pixels";
t.Units = "pixels";

figPos = fig.Position;
tPos = t.Position;

fig.Position = [figPos(1), figPos(2), figPos(3), figPos(4)+100];
t.Position = [tPos(1), tPos(2)+100, tPos(3), tPos(4)];

exportgraphics(gcf, "fig1.jpg", "Resolution", 600)

then we can get such an figure:

fig

which includes the whole x-label. And as can be seen, the figure gets taller.