Missing part of my figure when saving as pdf
178 views (last 30 days)
Show older comments
Jaime De La Mota Sanchis
on 30 Apr 2022
Commented: Jaime De La Mota Sanchis
on 2 May 2022
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
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';
Accepted Answer
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.
- 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.
- 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)
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!