2016-06-14 13 views
1

Ich habe ein df wie so:Plotten basierend auf groupby

Year Grass  Crop  Forest  Ecoregion CRP 
1993 30.41268857 68.45446692 0.2 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 

und ich möchte ein groupby auf Ecoregion Graphen basiert. Dann innerhalb jedes Ecoregion Ich möchte basierend auf einzigartigen CRP Graphen so dass jede einzigartige Ecoregion wird eine eigene PDF-Datei erhalten und dann in dieser Datei werden Grafiken basierend auf CRP. In diesem Fall hat Ecoregion46e drei Graphen (0508common, 05f08 und 05f97) und Ecoregion46k wird auch drei Graphen haben.

Ich versuche, den folgenden Code:

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) 

aber das funktioniert nicht richtig, die Grafiken sind nicht einmal Graphen bar, wie ich sie haben will.

Ein Beispiel dafür, wie man einzelne Graphen zu bekommen zu sein, wie ich es will mit diesem zu, aber das nutzt nicht den groupby wie ich es will: die df

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) 

in diesem Fall wäre:

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

Antwort

1

Sie hatten einen Fehler in der Handlung. Sie müssen die innere Gruppe (igr) nicht außen zeichnen. Ich hatte den Code sein glatten leicht verändert:

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) 

Eines der Ergebnisse: enter image description here

Verwandte Themen