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'

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.