1

I want to call a function that plots data in a pandas dataframe and appends the plot to an existing pdf file.

def plot_and_pdf(df,pdf_file):
   f, (ax1, ax2) = plt.subplots(2, 1,figsize=(15,8),sharex=True)
   pp = PdfPages(pdf_file)
   ax1.plot(df['col1'])
   ax2.plot(df['col2'])
   pp.savefig()
   pp.close()

The problem is that this overwrites the file, it does not append the figure to a new page.

The function gets called every time a given event takes place in the main program. I know that I may tweak the code in such a way that I create a 2nd pdf file and merge the two pdfs each time such an event happens, using pyPdf as in the accepted answer here, then deleting the excess pdf.

I'm looking for a more 'direct' way, without the workaround and merging, if it exists. As PdfPages is basically a wrapper around PdfFile, maybe there is a way from there but I couldn't figure it out from the source code.

user50473
  • 165
  • 1
  • 7

0 Answers0