在Matplotlib图

时间:2016-09-14 19:33:19

标签: python-2.7 pandas matplotlib

我有一个如下所示的数据框:

MTGP_value     MTGP        PHP_value    PHP          WMTGP_value       WMTGP
-0.0643        0           -0.0743      0.006004563  -0.0703           0
-0.0626        0.0052669   -0.072       0            -0.0685           0
-0.0609        0.00790035  -0.0697      0            -0.0667           0
-0.0592        0           -0.0673      0.012009127  -0.065            0
-0.0575        0           -0.065       0.012009127  -0.0632           0.002322503
-0.0559        0.0052669   -0.0627      0.006004563  -0.0614           0
-0.0542        0           -0.0604      0.006004563  -0.0597           0
-0.0525        0           -0.058       0.012009127  -0.0579           0.004645005
-0.0508        0.00790035  -0.0557      0            -0.0561           0.002322503
-0.0491        0.00790035  -0.0534      0.006004563  -0.0544           0.004645005
-0.0474        0.0105338   -0.0511      0.012009127  -0.0526           0.011612514
-0.0457        0.0105338   -0.0488      0.048036508  -0.0509           0.002322503
-0.0441        0.026334501 -0.0464      0.030022817  -0.0491           0.009290011
-0.0424        0.079003502 -0.0441      0.102077579  -0.0473           0.009290011
-0.0407        0.197508756 -0.0418      0.246187102  -0.0456           0.018580022
-0.039         0.474021015 -0.0395      0.444337697  -0.0438           0.034837541
-0.0373        0.94804203  -0.0371      0.942716465  -0.042            0.069675082
-0.0356        1.727543254 -0.0348      1.92146031   -0.0403           0.157930186
-0.034         3.046901746 -0.0325      3.39257836   -0.0385           0.336762896
-0.0323        4.371527138 -0.0302      6.004563468  -0.0367           0.68978331
-0.0306        6.133305243 -0.0279      8.148192626  -0.035            1.203056414
-0.0289        8.029389303 -0.0255      10.39389936  -0.0332           2.090252456
-0.0272        9.290811893 -0.0232      11.58880749  -0.0314           3.379241471
-0.0255        10.39949438 -0.0209      12.4594692   -0.0297           5.237243654
-0.0239        10.75501014 -0.0186      13.0178936   -0.0279           7.127760875
-0.0222        10.23358702 -0.0162      13.29410352  -0.0261           8.964860534
-0.0205        9.364548495 -0.0139      10.2557944   -0.0244           10.94131036
-0.0188        7.989887552 -0.0116      5.710339858  -0.0226           11.74025129
-0.0171        5.996365839 -0.00928     1.78335535   -0.0209           11.87727896
-0.0154        4.853448503 -0.00695     0.126095833  -0.0191           10.70209258
-0.0137        3.328680905 -0.00463     0            -0.0173           9.255173375
-0.0121        1.922418561 -0.00231     0            -0.0156           7.346076132
-0.0104        0.679430121 1.36E-05     0            -0.0138           5.371948812
-0.00869       0.094804203 0.00234      0            -0.012            2.552430499
-0.00701       0           0.00466      0            -0.0103           0.761780895
-0.00532       0           0.00698      0            -0.0085           0.092900109
-0.00364       0           0.0093       0            -0.00674          0
-0.00195       0           0.0116       0            -0.00497          0
-0.000269      0           0.0139       0            -0.00321          0
0.00142        0           0.0163       0            -0.00144          0
0.0031         0           0.0186       0.006004563  0.000321          0
0.00478        0           0.0209       0            0.00209           0
0.00647        0           0.0232       0            0.00385           0
0.00815        0           0.0256       0.006004563  0.00561           0
0.00984        0           0.0279       0            0.00738           0
0.0115         0           0.0302       0            0.00914           0
0.0132         0           0.0325       0            0.0109            0
0.0149         0           0.0349       0            0.0127            0
0.0166         0           0.0372       0            0.0144            0
0.0183         0.00263345  0.0395       0.006004563  0.0162            0.002322503

MTGPPHPWMTP表示相应列的频率(以百分比表示)。我想在x轴上绘制MTGP_valuePHP_valueMTGP_value的图表,并在y轴上绘制频率。我正在使用此代码:

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

with PdfPages(C:\out_path_way\graphs.pdf'))  as pdf:  
                         plot=df.plot(x=df[[col for col in df.columns if 'value' in col]].min(axis=1), y=['WMTGP', 'MTGP', 'PHP'],  title='GDD', lw=2.5)
                         plot.set_xlabel('SWIR32 Value')
                         plot.set_ylabel('Percent')
                         fig=plt.gcf()
                         plot.legend(loc='center left', bbox_to_anchor=(1, 0.5))
                         pdf.savefig(fig, bbox_inches='tight', pad_inches=0)
                         plt.close(fig)

返回: enter image description here

但是当我在Excel中制作图表时,没有指定x轴(只是绘制MTGPPHPWMTGP)我得到了这个,这就是我想要的看起来像:

enter image description here

我很确定我的代码中的问题是这一行:

plot=df.plot(x=df[[col for col in df.columns if 'value' in col]].min(axis=1), y=['WMTGP', 'MTGP', 'PHP'],  title='GDD', lw=2.5)

我要对该行进行的操作是确保MTGP_valuePHP_valueWMTGP_value的整个值范围都包含在x轴中,但它并不是&#39似乎是这样做的。

编辑: 问题可能会出现,因为我循环遍历多个文件并使用groupby制作图表。整个代码段是这样的:

import pandas as pd, os
from simpledbf import Dbf5
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np

out=r'F:\Sheyenne\Pixel_Regression\GAP_histos\intersects'
#                    
folders=r'F:\Sheyenne\Pixel_Regression\GAP_histos\intersects'
for folder in os.listdir(folders):

    if not os.path.isdir(os.path.join(out,folders,folder,'figures')):
        os.mkdir(os.path.join(out,folders,folder,'figures'))

    for sub in os.listdir(os.path.join(folders,folder)):
        if ('monthly' not in sub) and ('figures' not in sub):
            if not os.path.isdir(os.path.join(out,folders,folder,'figures',sub)):
                os.mkdir(os.path.join(out,folders,folder,'figures',sub))
#            
            for f in os.listdir(os.path.join(folders,folder,sub)):
                if '.dbf' in f:
                    table = Dbf5(os.path.join(folders, folder, sub,f))
                    df = table.to_dataframe()
                    df=df.iloc[:, (-1,-7,-6,-5)]
                    df=df[df.iloc[:,0] .isin (['planted herbaceous perennials', 'prairie - mesic tall grass', 'prairie - wheatgrass prairie', 'prairie - bluestem-needlegrass-wheatgrass', 'prairie - sand', 'wetland - palustrine seasonal', 'wetland - palustrine semipermanent', 'woodland - deciduous', 'wetland - palustrine temporary', 'prairie - wet-mesic tall grass', 'woodland - floodplain', 'shrubland - lowland deciduous', 'wetland - riverine'])]
                    replacements = {'planted herbaceous perennials': 'PHP', 'prairie - mesic tall grass': 'MTGP', 'prairie - wheatgrass prairie': 'WGP', 'prairie - bluestem-needlegrass-wheatgrass': 'BNW', 'prairie - sand': 'SP', 'wetland - palustrine seasonal': 'Wetland', 'woodland - deciduous': 'Forest', 'wetland - palustrine temporary': 'Wetland', 'prairie - wet-mesic tall grass' : 'WMTGP', 'woodland - floodplain': 'Forest', 'shrubland - lowland deciduous': 'Shrubland', 'wetland - riverine': 'Wetland', 'wetland - palustrine semipermanent': 'Wetland'}
                    df.replace(replacements, regex=True, inplace=True)
                    df=df[df.iloc[:,0] .isin (['WMTGP', 'MTGP', 'PHP'])]
                    df=df[df.iloc[:,3] .isin (['Yes'])]
                    list1=[]
                    for i, outgrp in df.groupby(['LAND_COVER']):
                         minimum=outgrp.iloc[:,1].min()
                         maximum=outgrp.iloc[:,1].max()
                         bins=np.linspace(minimum,maximum,51)
                         categories=pd.cut(outgrp.iloc[:,1],bins)
                         outgrp['categories']=pd.cut(outgrp.iloc[:,1], bins)
                         outgrp['Frequencies']=1
                         outgrp2=outgrp.groupby(['categories'])[['Frequencies']].sum().fillna(0).reset_index()
                         outgrp2['Frequencies']=outgrp2.Frequencies/outgrp2.Frequencies.sum()*100
                         outgrp2.rename(columns={'categories': i +'_value','Frequencies': i }, inplace=True)
                         outgrp2.iloc[:,0]=outgrp2.iloc[:,0].str.replace('(','')
                         outgrp2.iloc[:,0]=outgrp2.iloc[:,0].str.replace(']','')
                         outgrp2.iloc[:,0]=outgrp2.iloc[:,0].map(lambda x: x.split(',')[1])
                         outgrp2.sort_values(by= i + '_value', inplace=True)
                         list1.append(outgrp2)
                    frame=pd.concat(list1, axis=1)
                    with PdfPages(os.path.join(out,folders,folder,'figures',sub,sub+'_GAP_graphs.pdf'))  as pdf:  
                         plot=frame.plot(x=frame[[col for col in frame.columns if 'value' in col]].min(axis=1), y=['WMTGP', 'MTGP', 'PHP'],  title=folder.split('_')[0], lw=2.5)
                         plot.set_xlabel(sub + ' Value')
                         plot.set_ylabel('Percent')
                         fig=plt.gcf()
                         plot.legend(loc='center left', bbox_to_anchor=(1, 0.5))
                         pdf.savefig(fig, bbox_inches='tight', pad_inches=0)
                         plt.close(fig)

在此示例中,frame代表我原始示例中的df

0 个答案:

没有答案