matplotlib: merge 2 pdfs to one side-by-side
Question:
I have 2 different pdf plots (generated by matplotlib also) and I’d like to combine them as one side-by-side. Originally I wanted to generate with 2 subplots 121 and 122 but then was too tricky to adjust many details. So I generated two independent plots.
Is there any way to import those ready pdfs and just make one out of them? Because at the end in a latex file which I am using, it is much easier to deal with one figure file rather than two!
Answers:
If you are on Linux or Mac, the pdfjam program can do it.
pdfjam --nup 2x1 leftFig.pdf rightFig.pdf --outfile combinedFig.pdf
I would save the plots as PNGs and then create a PDF out the PNGs using ReportLab. For example:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
c = canvas.Canvas('report.pdf', pagesize=letter)
c.drawImage('filename1.png', 0,0)
c.drawImage('filename2.png', 0,100)
c.save()
https://web.archive.org/web/20150111073718/http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html
Or if you’re set on merging multiple PDFs than that’s already been answered here: Merge PDF files or you can merge image directly using PIL see here: How do you merge images into a canvas using PIL/Pillow?.
I have 2 different pdf plots (generated by matplotlib also) and I’d like to combine them as one side-by-side. Originally I wanted to generate with 2 subplots 121 and 122 but then was too tricky to adjust many details. So I generated two independent plots.
Is there any way to import those ready pdfs and just make one out of them? Because at the end in a latex file which I am using, it is much easier to deal with one figure file rather than two!
If you are on Linux or Mac, the pdfjam program can do it.
pdfjam --nup 2x1 leftFig.pdf rightFig.pdf --outfile combinedFig.pdf
I would save the plots as PNGs and then create a PDF out the PNGs using ReportLab. For example:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
c = canvas.Canvas('report.pdf', pagesize=letter)
c.drawImage('filename1.png', 0,0)
c.drawImage('filename2.png', 0,100)
c.save()
https://web.archive.org/web/20150111073718/http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html
Or if you’re set on merging multiple PDFs than that’s already been answered here: Merge PDF files or you can merge image directly using PIL see here: How do you merge images into a canvas using PIL/Pillow?.