0

I'm trying to write a plot from matplotlib to a pdf file but getting an error.

I'm creating a plot using matplotlib from a Pandas DataFrame like this:

bplot = dfbuild.plot(x='Build',kind='barh',stacked='True')

From the documentation: http://matplotlib.org/faq/howto_faq.html#save-multiple-plots-to-one-pdf-file

It seems like I should be doing it this way:

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages(r'c:\temp\page.pdf')
figure = bplot.fig
pp.savefig(figure)
pp.close()

I get this error:

AttributeError: 'AxesSubplot' object has no attribute 'fig'
sparrow
  • 10,794
  • 12
  • 54
  • 74

2 Answers2

1

The problem is that dfbuild.plot returns an AxesSubplot and not a Figure instance, which is required by the savefig function.

This solves the issue:

pp.savefig(bplot.figure)
languitar
  • 6,554
  • 2
  • 37
  • 62
0

I works when I do it this way.

pp = PdfPages(r'c:\temp\page.pdf')
dfbuild.plot(x=['Build','Opperator'],kind='barh',stacked='True')
pp.savefig()
pp.close()

From Saving plots to pdf files using matplotlib

Community
  • 1
  • 1
sparrow
  • 10,794
  • 12
  • 54
  • 74