我有这样的df:

Year    Grass       Crop        Forest      Ecoregion   CRP
1993    30.41268857 68.45446692 0.255632102 46e         0508common
2001    47.29988968 47.68577796 0.509939614 46e         0508common
2006    71.37357063 20.40485399 0.908684114 46e         0508common
1993    27.17246635 71.97582809 0.12611897  46k         0508common
2001    65.74087991 30.61323084 0.1229253   46k         0508common
2006    81.763099   12.4386173  0.180860941 46k         0508common
1993    30.83567893 68.14034747 0.737649228 46e         05f08
2001    59.45355722 35.68378142 0.354265748 46e         05f08
2006    64.98592643 28.61787829 0.339706881 46e         05f08
1993    28.38187702 71.40776699 0.080906149 46k         05f08
2001    81.90938511 15.4368932  0.118662352 46k         05f08
2006    86.3214671  9.207119741 0.172599784 46k         05f08
1993    18.46387279 80.77686402 0.270081631 46e         05f97
2001    41.23923454 53.1703113  0.605111585 46e         05f97
2006    65.30004066 25.45626696 0.989918731 46e         05f97
1993    20.34764075 78.68863002 0.218653535 46k         05f97
2001    55.42761042 39.96085063 0.191151874 46k         05f97
2006    76.34526161 16.53176535 0.246221691 46k         05f97


我想基于Ecoregion创建一个基于groupby的图形。然后在每个Ecoregion中,我要基于唯一的CRP进行图形绘制,因此每个唯一的Ecoregion将获得其自己的pdf文件,然后在该文件中将基于CRP绘制图形。在这种情况下,Ecoregion 46e将具有三个图形(0508common05f0805f97),而Ecoregion 46k也将具有三个图形。

我正在尝试以下代码:

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

df=pd.read_csv(r'C:\pathway_to_file.csv')
group=df.groupby(['Ecoregion'])
pdf_files = {}
out=r'C:\output_location'
for ecoregion, outer_group  in df.groupby(['Ecoregion']):
    with PdfPages(os.path.join(out,ecoregion + '.pdf')) as pdf:
                for crp, inner_group in outer_group.groupby(['CRP']):
                    title=crp + '_' + ecoregion
                    lu_colors=(['g','y','b','r', 'k'])
                    plot=group.plot(x=['Year'], y=['Grass', 'Crop', 'Forest'],kind='bar', colors=lu_colors, title=title).get_figure()
                    plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
                    plt.xticks(rotation=70)
                    plt.set_xlabel('Year')
                    plt.set_ylabel('Percent')
                    pdf.savefig(plot)
                    plt.close(plot)


但这不能正常工作,图形甚至都不像我想要的条形图。

如何使一个单独的图成为我想要的一个例子是这样,但这并没有像我想要的那样使用groupby:

with PdfPages(r'G:\graphs.pdf') as pdf:
        lu_colors=(['g','y','b','r', 'k'])
        ax=df.set_index('Year').plot(title='0508common_46e', kind='bar', colors=lu_colors)
        plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
        plt.xticks(rotation=70)
        ax.set_xlabel('Year')
        ax.set_ylabel('Percent')
        fig=plt.gcf()
        pdf.savefig(fig)
        plt.close(fig)


在这种情况下,df为:

    Year    Grass       Crop        Forest      Ecoregion   CRP
    1993    30.41268857 68.45446692 0.255632102 46e         0508common
    2001    47.29988968 47.68577796 0.509939614 46e         0508common
    2006    71.37357063 20.40485399 0.908684114 46e         0508common

最佳答案

你在情节上犯了一个错误。您必须绘制内部组(igr)而不是外部。我稍微修改了您的代码,使其更加流畅:

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

lu_colors=(['g','y','b','r','k'])

df = pd.read_csv('1.csv', header=0, usecols = [0,1,2,3,4,5])
for ecor, ogr  in df.groupby(['Ecoregion']):
    with PdfPages(os.path.join("./pdf", ecor.strip()+'.pdf')) as pdf:
        for crp, igr in ogr.groupby(['CRP']):
            title = crp.strip() + '_' + ecor.strip()
            plot = igr.plot(x=['Year'], y=['Grass', 'Crop', 'Forest'], kind='bar', colors=lu_colors, title=title).get_figure()
            plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
            plt.xticks(rotation=70)
            ax = plt.gca()
            ax.set_xlabel('Year')
            ax.set_ylabel('Percent')
            pdf.savefig(plot, bbox_inches='tight', pad_inches=0)
            plt.close(plot)


结果之一:
python - 根据groupby进行绘图-LMLPHP

关于python - 根据groupby进行绘图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37822419/

10-14 02:16