Search code examples
pythonmatplotlibpdfpages

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


I have a report to be submitted automatically and I am using matplotlib to do that. But I am not able to figure out how to create a blank page in the begining with Title in the middle on the type of analysis which is performed

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()

Solution

  • You should just be able to create a figure before your for loop with the title on. You also need to turn the axis frame off (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()