用python的Matplotlib绘图库批处理度分布图片

来源:互联网 发布:java中foreach 编辑:程序博客网 时间:2024/06/05 09:01
#coding:utf-8import os.pathimport matplotlib.pyplot as pltimport numpy as npimport pylab as plrootdir = "D:\\pythontest\\546"                                   # 指明被遍历的文件夹for parent,dirnames,filenames in os.walk(rootdir):    #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字    for filename in filenames:        print "parent is" + parent        print "filename is:" + filename        print "the full name of the file is:" + os.path.join(parent,filename)                file = open(os.path.join(parent,filename),"r")        f = 1;        deg = [0 for x in range(0, 600000)]        degcnt = [0 for x in range(0, 600000)]                maxdeg = 0;        while 1:            line = file.readline()            #print line,            if not line:                break            if line[0] == '*':                f = f ^ 1                continue            if f == 1:                a = 0                for p in line:                    if p >='0' and p <= '9':                        a=a*10+int(p);                    else:                        break                deg[a] += 1                #print 'deg[a]=',deg[a]                if(a > maxdeg):                    maxdeg = a                tagcnt = 0                b = 0                for p in line:                    if p == ' ':                        tagcnt += 1                        if tagcnt > 1:                            break                    if tagcnt == 1 and p >='0' and p <= '9':                        b=b*10+int(p)                deg[b] += 1                #print 'deg[b]=',deg[b]                if(b > maxdeg):                    maxdeg = b        print 'xxxxxxxxxxxxxxxx'                curr = 0        for i in deg:            if i > 0:                degcnt[i] += 1                if curr < i:                    curr = i                cnt = 0;        for j in degcnt:                if cnt > curr:                break            print j,            cnt += 1        print curr        print range(1, curr+1)        print degcnt[1:curr+1]        pl.loglog(range(1, curr+1), degcnt[1:curr+1],'o')# use pylab to plot x and y        #pl.show()# show the plot on the screen        name = parent + "\\" + filename + ".png"        print name        #pl.show()        pl.savefig(name)        <span style="color:#ff0000;">pl.close() </span>

如果不加p1.close这句话,那么生成的第二张图片变成了前两张图片的叠加,第三张图片变成了前三张图片的叠加,加入了该句话就分别得到了三张图片^_^

PS:代码写的挫,欢迎批评指正

0 0
原创粉丝点击