Save a Figure as a .png and .pdf of a Specific Size

27 ビュー (過去 30 日間)
Allen Hammack
Allen Hammack 2022 年 9 月 10 日
回答済み: Simon Chan 2022 年 9 月 11 日
I am trying to generate a plot and save it as .png and .pdf files of a certain size. I have many plots that I am generating that will eventually be put into a Word document. I have found that the plots I am generating appear differently depending on the computer I use. I expect that the reason the plot look differently based on the computer I generate them from is because the different computers have different sized monitors. Here is a simplified script that includes the commands I am using to generate my plot:
xdata = [1,2,3,4];
ydata = [1,4,1,2];
fig = figure('Units','normalized','Position',[0 0 1 1]);
set(gcf,'PaperPositionMode','Auto',...
'PaperUnits','inches',...
'PaperOrientation','landscape',...
'PaperSize',[15 15])
blue = [0 0.4470 0.7410];
title('Title','Fontsize',28,'Fontweight','bold','Interpreter','none');
xlabel('Time (min.)','FontSize',20,'Fontweight','bold')
ylabel('Data','FontSize',20,'Fontweight','bold')
ax_left = gca;
set(ax_left,'ycolor','k')
plot(xdata,ydata,'-o','Color',blue,'LineWidth',2);
xlim([0 5])
ylim([0 5])
ax = gca;
ax.FontSize = 20;
ax.FontWeight = 'bold';
box on
saveas(fig,fullfile(pwd,'figure.png','png');
print(fig,'-dpdf')
copyfile(figure_file,input_general{1}.all_plots_location);
I am using the "normalized" units option, so the plot appears large on my screen. I am trying to use the "PaperPositionMode," "PaperUnits," "PaperOrientation," and "PaperSize" options to export the plots to a different size that will not change regardless of the computer I use to generate my plots. Right now, I am using an arbitrary 15-inch by 15-inch paper size, but I'm not getting the .png or .pdf files to be that size. Can someone help?

回答 (1 件)

Simon Chan
Simon Chan 2022 年 9 月 11 日
Try this to see whether the following is what you want.
clear; clc;
f = figure;
plot(1:10,1:10);
% Set figure position after plotting. A higher value for higher resolution
% only, you may use a smaller value.
set(f,'Position',[0 0 15000 15000]);
% Set PaperSize and PaperPosition according to 15 inch on both sides
set(f,'PaperSize',[15 15],'PaperPosition',[0 0 15 15]);
% Print figure in png format. You may remove '-r1000' if you don't want to have high resolution image
print(f,'figure.png','-dpng','-r1000');
print(f,'figure.pdf','-dpdf');

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by