简体   繁体   中英

Error when trying to write a matplotlib plot from Pandas DataFrame to pdf

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'

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM