exportgraphics does not save exactly what I see in the plot

16 views (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Dec 2025 at 13:33
Edited: Fangjun Jiang on 16 Dec 2025 at 13:35
print('-image','-dpdf','example.pdf')
or
exportgraphics(gcf,'example.pdf', 'ContentType', 'image')

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!