I want to create a PDF file with data plots from many channels, using PdfPages. It works, but with large files it fills the memory until the program crashes. I found out, when I add an extra for loop, which creates additional subplots, the memory will be emptied periodically.
For example (maybe on other machines the data array must be larger to see the effect):
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
def timeplot(i, data, export_pdf):
dt = 1/2400
length = len(data)
data = data
time_array = np.arange(0,(dt*length),dt,dtype=np.double)
plt.ioff() #disable interactive mode
plt.figure(figsize=(15,8), dpi= 60, facecolor='w', edgecolor='k') #create figure
TimePlot = plt.subplot(211) #create subplot
plt.plot(time_array,data,linewidth=0.25, linestyle='-') #plot date
k = 1
#------- whith this loop "off", the memory will run full --------------
for l in range(0,5):
ax1 = plt.subplot(2,5,k+5)
k = k + 1
#-----------------------------------------------------------------------
export_pdf.savefig()
plt.close('all')
def main():
chn_count = 200
export_file_path="test.pdf"
with PdfPages(export_file_path) as export_pdf:
for i in range(0,chn_count):
data = np.random.rand(20000000)
timeplot(i, data, export_pdf)
if __name__ == "__main__":
main()
I don't need the extra subplots, but why is the memory running out, when only creating one plot per channel.
I also tried to empty the memory after each loop by using "multiprocessing.Process". That seemed to work, but the resulting PDF contained just data garbage.
Has anyone an idea, what could help?
The Program runs on: Ubuntu 20.04.3, Python 3.8.10, matplotlib 3.1.2, RAM: 16GB
Edit:
I tried the solution of this question as mentioned by Z Li.:
The original code works, but is only saving plots to multiple .png files. When I adapt the example to create a Multipage PDF, the memory also runs full, as in the code above.
So I tried to only create single page PDFs, and merge them all together at the end. But the memory is still running full.