简体   繁体   中英

Matplotlib: multipage PDF with rasterized plots

I generate a multipage pdf using the following code. Since I will have around 3000 plots, 10 per pages. the pdf could become uge. I though to substitute every plot with a jpg or pdf version. But I am not sure how to do it within PdfPage backend.

plots_per_page = 10
col_per_page = 1

with PdfPages('NewPDF.pdf') as pdf:
    for pgg in range(0,pages):

        fig = figure(figsize=(8,20) )
        gs1 = gridspec.GridSpec(plots_per_page//col_per_page, col_per_page)

        for i in arange(0,10):
            ax = fig.add_subplot(gs1[i])
            ax.bar(arange(20), random.normal(0,1,20), linewidth = 0.5)

        gs1.tight_layout(fig , h_pad=0.8, w_pad=0.8)
        axis('auto')

        savefig(pdf, format='pdf')
        close()

You have a similar problem to this question, I think: Rasterizing multiple elements in matplotlib

As in one of the answers there , use:

ax = fig.add_subplot(111, rasterized=True)

or do:

ax.set_rasterized(True)

You can then set the resolution for the plots if you want to nudge the filesize some more:

savefig(pdf, format='pdf', dpi=300)

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