Missing part of my figure when saving as pdf

조회 수: 25(최근 30일)
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
Jaime De La Mota Sanchis
Jaime De La Mota Sanchis 2022년 4월 30일
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)
>>

댓글을 달려면 로그인하십시오.

채택된 답변

Richard Quist
Richard Quist 2022년 5월 1일
편집: Richard Quist 2022년 5월 1일
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.
  댓글 수: 1
Jaime De La Mota Sanchis
Jaime De La Mota Sanchis 2022년 5월 2일
Thanks. This works just fine and is what I needed.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by