exportgraphics does not save exactly what I see in the plot

16 vues (au cours des 30 derniers jours)
Vitor
Vitor le 16 Déc 2025 à 12:58
Commenté : Vitor le 16 Déc 2025 à 14:56
Hi,
I am new to Matlab. Let me first explain what I want to do, and then my problem. First, I want to plot a line with two shaded intervals around it (basically, the impulse response with two confidence intervals). After reading some posts in this forum, I managed to do it (thanks a lot, Matlab community). Probably that is not the best way (I plot one shaded region over another), but it seems to work. Below I provide an example.
% 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');
After runing it, I get the following plot in Matlab.
But when I use exportgraphics to export the plot in PDF format (by using the code below), it does not save exactly what I see in the plot.
% Save
exportgraphics(gcf,'example.pdf', 'ContentType', 'vector');
clf('reset')
To be more precise, the plot contains the two shaded intervals (as you can see in the image above), but exportgraphics saves a PDF file with only one shaded interval (image below).
Can anyone help me, please? Thanks in advance.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 16 Déc 2025 à 13:33
Modifié(e) : Fangjun Jiang le 16 Déc 2025 à 13:35
print('-image','-dpdf','example.pdf')
or
exportgraphics(gcf,'example.pdf', 'ContentType', 'image')

Plus de réponses (0)

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by