Export plot with margins very close to edges

14 Ansichten (letzte 30 Tage)
Vitor
Vitor am 17 Dez. 2025 um 15:14
Kommentiert: Vitor am 17 Dez. 2025 um 22:25
Hi,
I am new to Matlab. My code below exports a plot with margins (default).
% Create values to plot
IR = [1 2 5; 5 3 8; 2 2 5]';
INF = IR-0.50;
SUP = IR+0.50;
INF2 = IR-1;
SUP2 = IR+1;
% Define the rows and columns for the subplots (you choose)
row = 2;
col = 2;
% Define a timeline
nsteps = size(IR, 1);
steps = 1:1:nsteps;
x_axis = zeros(1,nsteps);
x = steps;
% Options for plot
opt_plot.marker = 'o';
opt_plot.linecol = [13, 54, 84]./255;
opt_plot.swathecol = [138, 178, 212]./255;
opt_plot.linewidth = 2;
opt_plot.alpha = 0.5;
opt_plot.marker_x = '--k';
opt_plot.linewidth_x = 0.5;
FigSize(26, 24)
% var1
ii = 1;
y = IR(:,ii)';
ci1_u = SUP(:,ii)';
ci1_l = INF(:,ii)';
ci2_u = SUP2(:,ii)';
ci2_l = INF2(:,ii)';
subplot(row,col,1);
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on
patch([x fliplr(x)], [ci1_l fliplr(ci1_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
patch([x fliplr(x)], [ci2_l fliplr(ci2_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on % the same code line as above
plot(x_axis, opt_plot.marker_x, 'LineWidth', opt_plot.linewidth_x); hold on
xlim([1 nsteps]);
title('var1','FontSize',11);
set(gca, 'Layer', 'top');
% var2
ii = 2;
y = IR(:,ii)';
ci1_u = SUP(:,ii)';
ci1_l = INF(:,ii)';
ci2_u = SUP2(:,ii)';
ci2_l = INF2(:,ii)';
subplot(row,col,2);
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on
patch([x fliplr(x)], [ci1_l fliplr(ci1_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
patch([x fliplr(x)], [ci2_l fliplr(ci2_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on % the same code line as above
plot(x_axis, opt_plot.marker_x, 'LineWidth', opt_plot.linewidth_x); hold on
xlim([1 nsteps]);
title('var2','FontSize',11);
set(gca, 'Layer', 'top');
% var3
ii = 3;
y = IR(:,ii)';
ci1_u = SUP(:,ii)';
ci1_l = INF(:,ii)';
ci2_u = SUP2(:,ii)';
ci2_l = INF2(:,ii)';
subplot(row,col,3);
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on
patch([x fliplr(x)], [ci1_l fliplr(ci1_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
patch([x fliplr(x)], [ci2_l fliplr(ci2_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on % the same code line as above
plot(x_axis, opt_plot.marker_x, 'LineWidth', opt_plot.linewidth_x); hold on
xlim([1 nsteps]);
title('var3','FontSize',11);
set(gca, 'Layer', 'top');
% Save
exportgraphics(gcf,'example.pdf', 'ContentType', 'image');
clf('reset')
The resulting PDF file is a plot with margins, which I display below.
But I want the resulting plot in PDF file have really small margins, that is, the subplots be very close to the edges like in the PDF example below. How can I do it? Thanks in advance.

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 17 Dez. 2025 um 18:06
hello
there are different options to modify the subplots margins :
here I show you how to do with (you will have to dowload it and save it in your path)
with these paramaters for
% options for subtightplot
gap = [0.15,0.1]; % [vertical,horizontal]
marg_h = [0.05,0.05]; % [lower uppper]
marg_w = [0.03,0.05]; % [left right]
I got this result (you can continue to fine tune) :
Also I noticed that instead of repeating the same code for each subplot, you can make your code more concise with a for loop :
% Create values to plot
IR = [1 2 5; 5 3 8; 2 2 5]';
INF = IR-0.50;
SUP = IR+0.50;
INF2 = IR-1;
SUP2 = IR+1;
% Define the rows and columns for the subplots (you choose)
row = 2;
col = 2;
% Define a timeline
nsteps = size(IR, 1);
steps = 1:1:nsteps;
x_axis = zeros(1,nsteps);
x = steps;
% Options for plot
opt_plot.marker = 'o';
opt_plot.linecol = [13, 54, 84]./255;
opt_plot.swathecol = [138, 178, 212]./255;
opt_plot.linewidth = 2;
opt_plot.alpha = 0.5;
opt_plot.marker_x = '--k';
opt_plot.linewidth_x = 0.5;
FigSize(26, 24)
% options for subtightplot
gap = [0.15,0.1]; % [vertical,horizontal]
marg_h = [0.05,0.05]; % [lower uppper]
marg_w = [0.03,0.05]; % [left right]
for k=1:3
% var k
y = IR(:,k)';
ci1_u = SUP(:,k)';
ci1_l = INF(:,k)';
ci2_u = SUP2(:,k)';
ci2_l = INF2(:,k)';
subtightplot(row,col,k,gap,marg_h,marg_w);
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on
patch([x fliplr(x)], [ci1_l fliplr(ci1_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
patch([x fliplr(x)], [ci2_l fliplr(ci2_u)], opt_plot.swathecol, 'FaceAlpha', opt_plot.alpha, 'EdgeColor', 'none')
plot(x, y, 'color', opt_plot.linecol, 'Marker', opt_plot.marker, 'MarkerFaceColor', opt_plot.linecol, 'LineWidth', opt_plot.linewidth); hold on % the same code line as above
plot(x_axis, opt_plot.marker_x, 'LineWidth', opt_plot.linewidth_x); hold on
xlim([1 nsteps]);
title(['var' num2str(k)],'FontSize',11);
set(gca, 'Layer', 'top');
end
% Save
exportgraphics(gcf,'example.pdf', 'ContentType', 'image');
clf('reset')
  1 Kommentar
Vitor
Vitor am 17 Dez. 2025 um 22:25
Thanks for the solution and the advice to simplify my code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Mobile Fundamentals finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by