阅读背景:

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

来源:互联网 
#coding:utf-8
import os.path
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
rootdir = "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>#coding:utf-8
import os.path
import matplotlib.



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: