如何为使用matplotlib创建的PDF文档创建标题页

Bha*_*mar 6 python matplotlib pdfpages

我有一个报告要自动提交,我使用matplotlib来做到这一点.但我无法弄清楚如何在开始时使用标题创建一个空白页面,分析类型

with PdfPages('review_count.pdf') as pdf:
    for cat in self.cat_vars.keys():
        if len(self.cat_vars[cat]) > 1:
            plt.figure()
            self.cat_vars[cat].plot(kind='bar')
            plt.title(cat)
            # saves the current figure into a pdf page
            pdf.savefig()
            plt.close()
Run Code Online (Sandbox Code Playgroud)

tmd*_*son 5

您应该能够在for带有标题的循环之前创建一个图形。您还需要关闭轴框架 ( plt.axis('off'))。

with PdfPages('review_count.pdf') as pdf:
    plt.figure() 
    plt.axis('off')
    plt.text(0.5,0.5,"my title",ha='center',va='center')
    pdf.savefig()
    plt.close() 
    for cat in self.cat_vars.keys():
        if len(self.cat_vars[cat]) > 1:
            plt.figure()
            self.cat_vars[cat].plot(kind='bar')
            plt.title(cat)
            # saves the current figure into a pdf page
            pdf.savefig()
            plt.close()
Run Code Online (Sandbox Code Playgroud)