equ*_*ity 3 python matplotlib pdfpages seaborn
我正在尝试将多个数字保存到一个多页PDF文档中.我的代码如下:
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages('output.pdf')
sns.set_style('darkgrid')
g = sns.factorplot(data=df,
x='Date',
y='Product_Count',
col='Company',
col_wrap=4,
sharey=False)
g.set_xlabels('')
g.set_ylabels('product count')
g.set_xticklabels(rotation=45)
plt.locator_params(axis = 'x', nbins = 8)
f = sns.factorplot(data=df,
x='Date',
y='Volume_Count',
col='Company',
col_wrap=4,
sharey=False)
f.set_xlabels('')
f.set_ylabels('volume count')
f.set_xticklabels(rotation=45)
plt.locator_params(axis = 'x', nbins = 8)
figures = [g, f]
for figure in figures:
pdf.savefig(figure)
pdf.close()
Run Code Online (Sandbox Code Playgroud)
我看到此错误消息:
ValueError: No such figure: <seaborn.axisgrid.FacetGrid object at 0x237CD5F0>
Run Code Online (Sandbox Code Playgroud)
迭代有什么问题吗?
g并且f不是matplotlib.figure.Figure对象,它们是seaborn.axisgrid.FacetGrid对象(如@tcaswell在评论中所提到的).
PdfPages需要Figure实例,幸运的是它们很容易从FacetGrid对象中提取,使用g.fig和f.fig.
所以,你需要做的就是改变一行
figures = [g, f]
Run Code Online (Sandbox Code Playgroud)
至:
figures = [g.fig, f.fig]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
449 次 |
| 最近记录: |