OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Write multipage pdf report using BytesIO() stream in memory from matplotlib in python

  • Thread starter Thread starter Ali Mehdi
  • Start date Start date
A

Ali Mehdi

Guest
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.

Code:
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())
<p>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.</p>
<pre><code>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())
</code></pre>
 

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top