cho*_*oco 2 python matplotlib seaborn
我创建了一个使用 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文档。请帮忙,因为我可能不知道我做错了哪一部分
我认为您必须将您的情节分成几个数字,以便使用@r-beginners 提供的答案
如果您使用,catplot()您可以使用col_order=来指定要显示的主题。您可以遍历subjects使用itertools 的块。
像这样的东西:
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
from itertools import zip_longest
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
N_plots_per_page = 9
for cols in grouper(data['subject'].unique(), N_plots_per_page):
g = sns.catplot(data=data, x='solution', y='score', col='subject', col_wrap=3, kind='point', col_order=cols)
pdf.savefig(g.fig)
pdf.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |