简体   繁体   English

在matplotlib中将多个数字保存到一个pdf文件中

[英]Saving multiple figures to one pdf file in matplotlib

I have a code that creates about 50 graphs based on groupby . 我有一个代码,基于groupby创建大约50个图表。 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)

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. 当我希望将所有数据存储到一个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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