用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>

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值