MATLAB Answers

How to save a figure as a PDF with given aspect ration, resolution (and size)

29 views (last 30 days)
Benoît
Benoît on 8 Sep 2017
Commented: Cedric Wannaz on 8 Sep 2017
Hello,
I'm looking for a way to save my figures in a way that they can be properly inserted in latex (PDF is preferable but another format will also do). My requirements are the following:
1) Keep a predefined aspect ratio for the picture 2) Make all font sizes (ticks, labels, legend, ...) consistent with the image size. 3) Impose the resolution of the image
I tried several things and what worked best for me until now was
if true
pbaspect([1.5 1 1])
print(gcf,'Filename','-dpng','-r200');
end
Regarding 1), I used the command pbaspect to keep the aspect ratio of the plot constant and independent of the plot window size. This is OK Regarding 2), the font size is dependent of the resolution: the highest the largest the font size. How to avoid that? Regarding 3), I use '-r200' to impose a 200dpi but this doesn't work quite well with fonts as I mentioned above.
When trying to make a pdf I used
if true
set(fig,'PaperPositionMode','Auto','PaperSize',[p1(3), p1(4)+p2(4)])
print('AccuracyVsPerf','-dpdf','-r100') % code
end
However, the xlimit is not respected anymore, maybe because the aspect ratio I imposed doesn't allow it..
I must say that I tried a lot of different things with the available options with print, saveas, etc, and none of them worked as I wanted so I'm a bit lost and would appreciate any robust implementation for this purpose. Thanks!

Answers (1)

CARLOS RIASCOS
CARLOS RIASCOS on 8 Sep 2017

Hello friend, maybe the following code will serve you.

Basically what it does is to change the characteristics of the plot, putting a font (Times new roman), a font size and a resolution that you can choose The same code exports the image as .eps which is the best format for inserting images in LaTeX. You would get something like the following:

x=0:pi/20:6;
y1=sin(x); %function
figure(1);%ploting
plot(x,y1)
hold on
plot(x,-y1,'r')
title('PLOT')
xlabel('x')
ylabel('y')
grid on
 set(gca,'Units','normalized',... %characteristics of plot.
    'Xtick',0:1:x(end)+1,'YTick',-1.2:.2:1.2,...
    'FontUnits','points','FontWeight','normal',...
    'FontSize',11,'FontName','Times');
       %(format export,name file, resolution
print ('-depsc', 'export_hgraph', '-r200')end

Tags

Community Treasure Hunt

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

Start Hunting!