为什么我要在python中得到重叠的饼图

2024-10-03 11:17:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用walk函数>;搜索.cnt文件>;计算感兴趣的值>;在饼图中绘制它们。但问题是,当我的prog处理第一个文件夹和绘图时,它会起作用好吧,但是在那之后,无论它读到什么文件夹和打印它都会重叠。无法理解我的代码中是否有bug。我以前喜欢:

for root,dirs,files in os.walk(path):
    aspCount = 0
    gluCount = 0
    aspCountCol1 = 0
    aspCountCol2 = 0
    gluCountCol1 = 0
    gluCountCol2 = 0

    listOfFile = glob.iglob(os.path.join(root,'*.cnt'))
    for filename in listOfFile:
        inp = open(filename,'r').read().strip().split('\n')
        for line in map(str.split,inp):

            k = line[-1]
            m = line[0]
            if k == 'ASP':
               aspCountCol1 += 1
            elif m == 'ASP':
               aspCountCol2 += 1
            if k == 'GLU':
               gluCountCol1 += 1
            elif m == 'GLU'
               gluCountCol1 +=1
                     # here lies the problem for me !!!!
        aspCount = aspCountCol1 + aspCountCol1
        gluCount = gluCountCol1 + gluCountCol1
        #now plotting......
        from pylab import *
        figure(1, (figsize=(8,8))
        labels = 'asp','glu'
        fracs = [asp_count,glu_count]
        pie(fracs,explode=None,labels=labels,autopct='%1.1f%%',shadow=False)
        c = 'fig.png'
        savefig(os.path.join(root,c))

现在,问题是:如果使用此代码处理包含.cnt文件的不同子文件夹的目录,它将不会出错。但是第一个文件夹生成的图形可以,但是当prog处理下一个文件夹时,它成功地处理了数据,但是生成的图形与上一个文件夹重叠。
我正在处理的文件是:

LYS  ARG
ASP  GLU
GLU  SAP
JAS  ASP
SAK  GLU

Tags: 文件pathingt文件夹forlabelsos