Missing part of my figure when saving as pdf

192 views (last 30 days)
Some time ago I made a post in whichI asked for a better optimization of the space when saving a figure.
My code now looks as follow:
figure ('Position',[0 0 1200 400]);
subplot(1,2,1)
contourf(Y,X, mediaU3D)
hold on
axesm mercator;
plot(coastlon,coastlat,'k', 'LineWidth', 1.5)
axis([-20 -10 23 32])
c2 = colorbar;
xlabel('Longitude(^o)')
ylabel('Latitude(^o)')
title('X-direction')
subplot(1,2,2)
contourf(Y,X, mediaV3D)
hold on
axesm mercator;
plot(coastlon,coastlat,'k', 'LineWidth', 1.5)
axis([-20 -10 23 32])
c2 = colorbar;
xlabel('Longitude(^o)')
ylabel('Latitude(^o)')
title('Y-direction')
sgtitle('Mean wind velocity (m/s) realizations')
However, when I try to save the figure manually by pressing the save button, see figure1
and try to save it as a pdf. Then, unfortunately I get the result that can be seen in the attached .pdf file, the colorbar of the right subfigure is missing.
Can someone please tell me what am I doing wrong?
Best regards.
Jaime.
  2 Comments
Riccardo Scorretti
Riccardo Scorretti on 30 Apr 2022
Edited: Riccardo Scorretti on 30 Apr 2022
Can you post also the variables required to execute the code, so we can play with it? I'm unable to reproduce with randomly generated data.
That's being said, this problem could be linked with paper orientation. Perhaps you could try this before saving the image:
fig = gcf ; fig.PaperOrientation = 'landscape';
Jaime De La Mota Sanchis
Jaime De La Mota Sanchis on 30 Apr 2022
This should do the part as a reproducible example.
clear
close all
clc
XVec=[23:0.5:32];
YVec=[-10:-0.5:-20];
YVec=YVec';
X=repmat(XVec, 21, 1);
Y=repmat(YVec, 1, 19);
stdU3D=normrnd(10, 5, [21, 19]);
stdV3D=normrnd(6, 3, [21, 19]);
figure ('Position',[0 0 1200 400]);
subplot(1,2,1)
contourf(Y,X, stdU3D)
hold on
axesm mercator;
axis([-20 -10 23 32])
c2 = colorbar;
xlabel('Longitude(^o)')
ylabel('Latitude(^o)')
title('X-direction')
subplot(1,2,2)
contourf(Y,X, stdV3D)
hold on
axesm mercator;
axis([-20 -10 23 32])
c2 = colorbar;
xlabel('Longitude(^o)')
ylabel('Latitude(^o)')
title('Y-direction')
sgtitle('STD wind velocity (m/s) realizations')
I also get a warning message in orange on my prompr that reads:
Warning: The figure is too large for the page and will be cut off. Resize the figure, adjust the output size by
setting the figure's PaperPosition property, use the 'print' command with either the '-bestfit' or '-fillpage'
options, or use the 'Best fit' or 'Fill page' options on the 'Print Preview' window.
> In validate (line 146)
In print (line 67)
In hgexport
In filemenufcn>localSaveExportHelper (line 221)
In filemenufcn>localSaveExport (line 346)
In filemenufcn (line 58)
>>

Sign in to comment.

Accepted Answer

Richard Quist
Richard Quist on 1 May 2022
Edited: Richard Quist on 1 May 2022
When using File -> Save As, MATLAB saves the figure to a "page" when saving the figure to PDF. As the warning mentions, your figure is too wide for the page size, which is why it is being cut off. There are a couple of approaches you can take to get around this issue.
  1. If you are using R2020a or later, you can use the exportgraphics command from the MATLAB command line to produce a tightly cropped PDF. See below for an example of how to do this.
  2. Before saving the figure you can set the figure's PaperSize to match the figure's Position, so that the PDF page size will be large enough to hold the entire content. See below for an example of how to do this.
Example using exportgraphics:
% This example assumes that 'fig' is the handle to your figure.
% In R2020a and later, you can use the exportgraphics command and specify
% the 'ContentType' as 'vector' (to ensure tightly cropped, scalable output):
exportgraphics(fig, 'output.pdf', 'ContentType', 'vector');
Example setting the figure's PaperSize before saving the figure:
% This example assumes that 'fig' is the handle to your figure.
% The figure's PaperSize property controls the page size for PDF output
% set the figure Units to match the PaperUnits, but first, save the
% figure's Units value so we can restore it when done
origUnits = fig.Units;
fig.Units = fig.PaperUnits;
% set the Page Size (figure's PaperSize) to match the figure size in the Paper units
fig.PaperSize = fig.Position(3:4);
% restore the original figure Units
fig.Units = origUnits;
% Now, when you save the figure it should not be cut off.

More Answers (0)

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!