8

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()
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Bharath Kumar
  • 197
  • 2
  • 11

1 Answers1

6

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()
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • 1
    Thanks it worked!. In addition to this if u add 'plt.axis('off')' it will also remove the axis – Bharath Kumar Sep 02 '15 at 09:28
  • 1
    @BharathKumar thanks, I updated the answer with that. Also realised you need to set the vertical and horizontal alignment to get the title truly in the center – tmdavison Sep 02 '15 at 09:42