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

How to create a title page for a PDF document created using matplotlib

我有一份要自动提交的报告,我正在使用 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()

您应该能够在 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()