在matplotlib中将多个数字保存到一个pdf文件中 [英] Saving multiple figures to one pdf file in matplotlib

查看:47
本文介绍了在matplotlib中将多个数字保存到一个pdf文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 groupby 创建大约 50 个图表的代码.代码如下所示:

I have a code that creates about 50 graphs based on groupby. The code looks like this:

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('foo.pdf') as pdf:
   for i, group in df.groupby('station_id'):
       plt.figure()


fig=group.plot(x='year', y='Value',title=str(i)).get_figure()
pdf.savefig(fig)

当我希望将所有数字存储到一个 pdf 时,这仅保存了一个数字(我系列中的最后一个).任何帮助将不胜感激.

This is saving only one figure, (the last one in my series) when I would like all of my figures to be stored into one pdf. Any help would be appreciated.

推荐答案

您的代码中存在缩进错误.由于您的绘图命令不在循环中,因此只会创建最后一个绘图.

There is an indentation error in your code. Since your plotting command was not in the loop, it will only create the last plot.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('foo.pdf') as pdf:
   for i, group in df.groupby('station_id'):
       plt.figure()
       fig=group.plot(x='year', y='Value',title=str(i)).get_figure()
       pdf.savefig(fig)

这篇关于在matplotlib中将多个数字保存到一个pdf文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆