0

I am new to python/coding and apologize if my question is too basic to have an answer available online as i am having a hard time finding the solution to my problem. I want to append multiple BytesIO() objects together and later write them as a single pdf file. Below is the code that i have managed so far. The tmp3 object bytes are equal to sum of tmp and tmp2 objects, however when i write tmp3 to disk, i get a single page pdf file with only the tmp2 figure. Please let me how can i manage to get a pdf with both the figures and on separate pages. Any suggestions involving a totally different approach are also welcomed.

cols = data.columns.values
col = cols.tolist()
g = sns.PairGrid(data[col])
g.map_diag(sns.histplot) #, hue=data['tar'], color='.9'
g.map_upper(sns.scatterplot) #, hue=data['tar']
g.map_lower(sns.kdeplot,cmap="Set2")

tmp = BytesIO()       
plt.savefig(tmp, format='pdf')
t=tmp.tell()
tmp.seek(0)

tmp2 = BytesIO()
fig, ax = plt.subplots(figsize=(20,15))
sns.heatmap(data.corr(),cmap='coolwarm', annot=True, fmt=".1f", ax=ax)
plt.savefig(tmp2,format='pdf')
p=tmp2.tell()
tmp2.seek(0)

tmp3 = BytesIO()
s = tmp3.tell()
tmp3.write(tmp.getvalue())
tmp3.write(tmp2.getvalue())
ss = tmp3.tell()
tmp3.seek(0)

temporarylocation="pairgrid.pdf"
with open(temporarylocation,'wb') as out:
    out.write(tmp3.read())
6
  • Duplicate of stackoverflow.com/questions/21364405/… ? Commented Aug 9, 2021 at 15:04
  • No, i want to generate the plots in memory using BytesIO() and then combine those bytes of plots and write them as a single file in pdf. Saving the plots in memory using BytesIO() is crucial for the application. Thankyou Commented Aug 9, 2021 at 18:40
  • You can open your PDfPages document as a BytesIO Commented Aug 9, 2021 at 19:26
  • Yes i am doing that in tmp and tmp2, but how do i combine those bytes and write them in a single pdf file. That is my question. Commented Aug 10, 2021 at 10:14
  • 1
    You cannot concatenate PDF files just using the raw files. You need to assemble them, as PDfPages does. What you are doing is the command line equivalent of cat first.pdf second.pdf > result.pdf. And that will not create a valid pdf. Commented Aug 10, 2021 at 14:30

1 Answer 1

0

Herewith a snippet (tested on python 3.10):

import io
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

merge_file = 'your.pdf'

pdf_bytes = io.BytesIO()
pdf_report = PdfPages(pdf_bytes)

something = [1,3,5,6,3]
plt.plot(something)
pdf_report.savefig()
plt.close()

something = [3,3,10,-6,5]
plt.plot(something)
pdf_report.savefig()
plt.close()

pdf_report.close() # close before saving

pdf_bytes.seek(0) # rewind

# Some hypothetical writer
io_handler = SomeBytesHandler()
io_handler.upload_bytes(merge_file, pdf_bytes)
"""
For the above I assume you want to store on a server and not on the host OS

On OS you would store like:

with open(merge_file, "wb") as f:
    f.write(pdf_bytes.getbuffer())
"""
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.