绘制基于groupby分离pdf文件

时间:2015-11-15 22:50:31

标签: python pandas matplotlib

我有一个这样的数据框:

    NDVI   Value   Allotment   Date
0      0  0.208430  Arnstson  19840517
1      0  0.211430  Arnstson  19840517
2      0  0.214430  Arnstson  19840517
3      2  0.217430  Arnstson  19840517
4      4  0.220430  Arnstson  19840517
5      1  0.223430  Arnstson  19840517
6      6  0.226430  Arnstson  19840517
7      1  0.229430  Arnstson  19840517
8     11  0.232430  Arnstson  19840517
9     13  0.235430  Arnstson  19840517
10    17  0.238430  Arnstson  19840517
11     9  0.241430  Arnstson  19840517
12     9  0.244430  Arnstson  19840517
13     7  0.247430  Arnstson  19840517
14    22  0.250430  Woodlot   19840517
15    17  0.253430  Woodlot   19840517
16    14  0.256430  Woodlot   19840517
17     5  0.259430  Woodlot   19840517
18    14  0.262430  Woodlot   19840517
19    19  0.265430  Woodlot   19840517
20    10  0.268430  Woodlot   19840517
21    11  0.271430  Arnstson  19840518
22    10  0.274430  Arnstson  19840518
23     9  0.277430  Arnstson  19840518
24     9  0.280430  Arnstson  19840518
25     5  0.283430  Woodlot   19840518
26     7  0.286430  Woodlot   19840518
27     1  0.289430  Woodlot   19840518
28    11  0.292430  Woodlot   19840518
29     6  0.295430  Woodlot   19840518

我希望根据Allotment创建发送到不同pdf文件的图表。因此,我希望将包含唯一Allotment个名称的所有绘图发送到一个文件,该文件针对每个相应的NDVI绘制ValueDate。我可以使用以下代码轻松地为个人Allotment执行此操作:

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

group=df.groupby(['Allotment'])
Arnstson=group.get_group('Arnstson')

with PdfPages(r'C:\delete.pdf') as pdf:
    for i, group in Arnstson.groupby(['Allotment', 'Date']):
        plot=group.plot(x='Value', y='NDVI', title=str(i)).get_figure()
        pdf.savefig(plot)  
        plt.close(plot)

但我在Allotment中有53个唯一名称,并且不希望单独选择所有这些名称。

1 个答案:

答案 0 :(得分:1)

一种策略是打开所有PDF文件,写入相应的文件,然后关闭它们。在这里,我使用字典来跟踪Allotment是关键字的文件句柄。我把它们全部写完,然后在一个单独的步骤中关闭所有文件句柄。

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

pdf_files = {}
for group_name, group in df.groupby(['Allotment', 'Date']):
    allotment, date = group_name
    if allotment not in pdf_files:
        pdf_files[allotment] = PdfPages('C:\\' + allotment + '.pdf') 
    plot=group.plot(x='Value', y='NDVI', title=str(group_name)).get_figure()
    pdf_files[allotment].savefig(plot)
    plt.close(plot)

for key in pdf_files:
    pdf_files[key].close()

另一种方法是使用嵌套组,其中外部groupby使用Allotment,内部(使用外部组)使用Date。这样就可以一次打开和关闭一个文件,如果有Allotment个可能很多,那就更好了。

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

pdf_files = {}
for allotment, outer_group  in df.groupby(['Allotment']):
    with PdfPages('C:\\' + allotment '.pdf') as pdf:
        for date, inner_group in outer_group.groupby(['Date']):
            plot=group.plot(x='Value', y='NDVI', title=str(allotment, date)).get_figure()
            pdf.savefig(plot)
            plt.close(plot)

这个版本稍微短一点,虽然它确实涉及嵌套循环。我更喜欢第二种,因为它看起来更清晰。