简体   繁体   中英

Why am i getting "memory error" in reportlab while generating the pdf

I have been trying to generate a pdf using reportlab. When i put 4 images, It works. But I put just 5 images it shows memory error. Please help.

Here is the code:

def plot_graph():
    plt.figure(figsize=(8, 5.25))

    stats = data.resample('D', on='DATE').count()['DATE']
    stats.plot(figsize=(12, 12))
    plt.xlabel('Dates', fontsize=16)
    plt.ylabel('Number of People', fontsize=16)
    plt.grid()

    buf = io.BytesIO()
    plt.savefig(buf, format='png', dpi=300)
    buf.seek(0)

    return buf




def plot_graph1():
    plt.figure(figsize=(8, 5.25))

    stats = data.resample('D', on='DATE').count()['DATE']
    stats.plot(figsize=(12, 12))
    plt.xlabel('Dates', fontsize=16)
    plt.ylabel('Number of People', fontsize=16)
    plt.grid()

    buf = io.BytesIO()
    plt.savefig(buf, format='png', dpi=300)
    buf.seek(0)

    return buf1

Similarly for plot 2,3,4. This makes total 5 plots.

image_buffer1 = plot_graph()
im = Image(image_buffer1, 8 * inch, 5.25 * inch)
Story.append(im)

add_text("Description of the chart.")

image_buffer1 = plot_graph1()
im2 = Image(image_buffer2, 8 * inch, 5.25 * inch)
Story.append(im2)

add_text("Description of the chart.")

Similarly for plot 2,3,4. This makes total 5 plots.

Please help. What is wrong with my code?

error:

 name = _digester(rawdata+mdata)
MemoryError

The most likely reason you are running out of memory is beacause you aren't closing your plt .

I use this function to save and draw my plots in my PDFs:

def save_and_draw(fig, x_img, y_img, width_img=width_img, height_img=height_img):

    imgdata = BytesIO()
    fig.savefig(imgdata, format="png")
    imgdata.seek(0)
    imgdata = ImageReader(imgdata)

    self.c.drawImage(imgdata, x_img, y_img, width_img, height_img)
    plt.close(fig)

Some of them have more than 15 plots and everything works fine for me. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM