先找到所有txt文件的路径,具体详情的可以点击这里。获得其所有路径后,遍历所有路径下的所有txt文件,按区间分类txt文件中的数值,并利用matplotlab画其bar图形。
其中txt文件中的数据格式是(只读取每行的后面的两个):
其实现代码如下:
import os
import numpy as np
import matplotlib.pyplot as plt
root = "H:\\ab123"
def findtxt(path, ret):
"""Finding the *.txt file in specify path"""
filelist = os.listdir(path)
for filename in filelist:
de_path = os.path.join(path, filename)
if os.path.isfile(de_path):
if de_path.endswith(".txt"): #Specify to find the *.txt file
ret.append(de_path)
else:
findtxt(de_path, ret)
def readtxt(path):
"""reading *.txt file in specify path"""
try:
txtarr = np.loadtxt(path)
except Exception:
txtarr = np.loadtxt(path, delimiter=',')
return txtarr
def category(val, catarr):
if val < 50:
catarr[0] += 1
if 51 <= val <= 100:
catarr[1] += 1
if 101 <= val <= 150:
catarr[2] += 1
if 151 <= val <= 200:
catarr[3] += 1
if 201 <= val <= 250:
catarr[4] += 1
if 251 <= val <= 300:
catarr[5] += 1
if 301 <= val <= 350:
catarr[6] += 1
if val > 350:
catarr[7]
return catarr
def autolabel(ax, rects, xpos='center'):
xpos = xpos.lower() # normalize the case of the parameter
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
'{}'.format(height), ha=ha[xpos], va='bottom')
def drawbar(arr1, arr2, xlab):
ind = np.arange(len(arr1)) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width / 2, arr1, width, color='SkyBlue', label='width')
rects2 = ax.bar(ind + width / 2, arr2, width, color='IndianRed', label='high')
ax.set_ylabel('Value')
ax.set_title('Target size width and height distribution')
ax.set_xticks(ind)
ax.set_xticklabels(xlab)
ax.legend()
autolabel(ax, rects1) # autolabel(rects1, "left")
autolabel(ax, rects2) # autolabel(rects2, "right")
plt.savefig('Target size width and height distribution', bbox_inches='tight')
plt.show()
txt_pathlist = []
xlab = ["<50", "50-100", "101-150", "151-200", "201-250", "251-300", "301-350", ">350"]
widthval = np.zeros(len(xlab))
findtxt(root, txt_pathlist)
highval = np.zeros(len(xlab))
for path in txt_pathlist:
if os.path.getsize(path):
txtarr = readtxt(path)
for lineval in txtarr:
widthval = category(lineval[2], widthval)
highval = category(lineval[3], highval)
drawbar(widthval, highval, xlab)
其结果如下: