我创建了一个使用 seaborn catplot 生成大量图表的程序。这是我的代码示例,其中说明了最终图表的外观。
plot= sns.catplot(data=df3, x="solutions", y="score", col="subject", col_wrap=3, hue="Value",height=3, aspect=1.5,legend=False, sharex=False, sharey=False)
plt.legend(loc='upper left')
plot.set_xticklabels(rotation=90)
plt.tight_layout()
#Create output
plot.savefig("output3.pdf")
Run Code Online (Sandbox Code Playgroud)
然而,由于绘图可能会扩展到 300 多个绘图,当我尝试导出为 pdf 时,图表的大小太大,并且大部分绘图被裁剪掉了。我注意到这个 pdf 输出只有 1 页。有没有办法为此输出创建多个页面?
编辑:
正如评论所建议的,我正在尝试使用 PdfPages
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
for fig in range(1, plt.gcf().number + 1):
plot= sns.catplot(data=df3, x="solutions", y="score", col="subject", col_wrap=3, hue="Value",height=3, aspect=1.5,legend=False, sharex=False, sharey=False)
plt.legend(loc='upper left')
plot.set_xticklabels(rotation=90)
plt.tight_layout()
pdf.savefig( fig )
pdf.close()
Run Code Online (Sandbox Code Playgroud)
但它返回错误消息:
<Figure size 432x288 with 0 Axes>
Run Code Online (Sandbox Code Playgroud)
并返回带有空白页的pdf文档。请帮忙,因为我可能不知道我做错了哪一部分