0
votes

I have a figure with 2x3 subplots in Matlab where each subplot contains 2 histograms. I typically save Matlab figures in eps but because doing so removes the transparency between the histograms in each subplot, I resorted to saving as pdf.

Currently, this is how I'm saving the file:

figure;
set(gcf,'Position',[100 100 1400 500])
set(gcf,'PaperPositionMode','auto')
print(gcf, 'filename.pdf', '-dpdf','-r0');

However, it produces a pdf file in portrait layout which means some subplots get cut out.

Now, I've been scouring for answers online and the recommended approach to fix my problem is given in this link.

Following the approach, I don't have any subplots cut out but the problem is that this forces the figure to fill-in the space of my pdf. In otherwords, my histograms are stretched out to fill the space of the pdf file in landscape format. I'd simply prefer a pdf file that has exactly the same proportions as my figure size above which is set(gcf,'Position',[100 100 1400 500]). This is how it works with eps so I'd like to have the same behavior with pdf.

Can anyone help me to fix it?

2

2 Answers

2
votes

To make sure the output is landscape:

set(gcf,'PaperOrientation','landscape')

Then use the "bestfit" option. That will scale it as large as possible but maintain the aspect ratio.

print(gcf, 'filename.pdf', '-dpdf','-r0','-bestfit')

There some more good info on Matlab's website

0
votes

MATLAB’s PDF output is always on a full A4 or Letter-sized page by default. With 'PaperPositionMode' set to 'auto', this does not change. For other output file formats (e.g. EPS) that setting causes the page size to be adjusted to the figure size. Not so for PDF. You need to manually set the 'PaperPosition' and 'PageSize' properties.

This File Exchange submission handles this for you (I’m the author).