Matplotlib:具有栅格化图的多页PDF [英] Matplotlib: multipage PDF with rasterized plots

查看:180
本文介绍了Matplotlib:具有栅格化图的多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码生成多页pdf. 由于我将拥有约3000个地块,因此每页10个. pdf可能会变成uge. 我虽然用jpg或pdf版本替换了每个图.但是我不确定如何在PdfPage后端中执行此操作.

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

那里的答案之一所示,请使用:

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

或这样做:

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)

这篇关于Matplotlib:具有栅格化图的多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆