1
$\begingroup$

I have 8 plot, just for sample data

p1 = Plot[x, {x, 1, 5}];
p2 = Plot[x^2, {x, 1, 5}];
p3 = Plot[x^3, {x, 1, 5}];
p4 = Plot[x^4, {x, 1, 5}];
p5 = Plot[x^5, {x, 1, 5}];
p6 = Plot[x^6, {x, 1, 5}];
p7 = Plot[x^7, {x, 1, 5}];
p8 = Plot[x^8, {x, 1, 5}];

which I want to export in PDF as 4 plots in each page. So I tried

g1=GraphicsGrid[{{p1, p2}, {p3, p4}}]; 
g2=GraphicsGrid[{{p5, p6}, {p7, p8}}];
Export["plot.pdf", {g1, g2}]

where p's are my plots. However this code produces a PDF file as all plots are in a single page. I searched Mathematica SE and just found

nb = CreateDocument[ExpressionCell[#, PageBreakBelow -> True] & /@ g1];
Export["plots.pdf", nb]

but this didn't work or maybe I didn't know how to use it. Any idea?

$\endgroup$
0
2
$\begingroup$

Modifying answer provided by Rom38

Clear["Global`*"]

plts = Plot[x^#, {x, 1, 5}] & /@ Range[8];

nb = CreateDocument[
   ExpressionCell[#, PageBreakBelow -> True] & /@ (GraphicsGrid /@ 
      ArrayReshape[plts, {2, 2, 2}])];

Export["/Users/roberthanlon/Downloads/plots.pdf", nb]

(* "/Users/roberthanlon/Downloads/plots.pdf" *)

EDIT: If instead of 2 by 2 arrays you want 4 by 1 arrays

plts = Plot[x^#, {x, 1, 5},
     PlotLabel -> StringForm["n = ``", #]] & /@ Range[8];

nb = CreateDocument[
   ExpressionCell[#, 
      PageBreakBelow -> True] & /@ (GraphicsGrid[#, ImageSize -> 360] & /@ 
      ArrayReshape[plts, {2, 4, 1}])];

Export["/Users/roberthanlon/Downloads/plots.pdf", nb, ImageSize -> 500]

(* "/Users/roberthanlon/Downloads/plots.pdf" *)
$\endgroup$
2
  • 1
    $\begingroup$ Thanks a lot. However there is a tiny problem: 4 plots have not been fitted to the size of page and just occupied the upper half of the page, how can I resize them best fitted in a page or use landscape style for PDF file to best fit? $\endgroup$
    – Wisdom
    Sep 3 '21 at 5:15
  • $\begingroup$ @Wisdom A "tiny" problem... AFAIK, up to now, there is no solution without using third-party software like Poppler. You can export every page as a separate PDF file, and then combine them using pdfunite (a part of Poppler utilities). $\endgroup$ Sep 3 '21 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.