3

My code reads all the text file in the folder, turns it into an array and plots a histogram of each data using a for cycle, pretty simple stuf, but I want to save all it returns, all the histograms into a single pdf file. I know the function:

plt.savefig('file.pdf')

works but since it's in a for cycle it will only save the last one, I mean it overwrites on it. Is there a function to add the other histograms or should I change the code?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
bajotupie
  • 165
  • 1
  • 2
  • 11
  • 1
    String `'file.pfd'` is the file name. Your program has full control over it. Make iteration number a part of it (e.g., `'file123.pfd'`), and each plot will be saved into its own file. – DYZ Apr 01 '17 at 22:00
  • You can combine multiple pdfs into one. See [Merge PDF files](http://stackoverflow.com/questions/3444645/merge-pdf-files) and [Merging multiple pdf files into one pdf](http://stackoverflow.com/questions/17104926/pypdf-merging-multiple-pdf-files-into-one-pdf) – Peter Wood Apr 01 '17 at 22:07
  • @Peter Wood I know, I just wanted to do it all at once. – bajotupie Apr 01 '17 at 22:11
  • @bajotupie I didn't know you knew. How could I? – Peter Wood Apr 01 '17 at 22:42

2 Answers2

2

You essentially have two options.

1. Create multiple pdf files, merge them afterwards

2. Write all plots directly to a single pdf file.

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

Another possibility is to export the histograms to images with Matplotlib and add them to a PDF file with ReportLab. This is a workaround, but you can tailormade your PDF report with all the histograms.

Strijder
  • 46
  • 2
  • post some export code how you would do it o validate the posting of the answer. End of review. – ZF007 Aug 14 '20 at 08:49