您的位置:首页 > 编程语言 > PHP开发

Matplotlib以小时为单位统计文件数据

2015-10-27 17:16 671 查看

Matplotlib 统计数据

统计文件中以小时hour为单位的数据数量

1.文件

文件内容如下:

2015-10-27 09:11:22
2015-10-27 00:11:22
2015-10-27 15:11:22
2015-10-27 02:11:22
2015-10-27 19:11:22
2015-10-27 04:11:22
2015-10-27 05:11:22
2015-10-27 06:11:22
2015-10-27 07:11:22
2015-10-27 09:11:22
2015-10-27 01:11:22
2015-10-27 01:11:22
2015-10-27 01:11:22
2015-10-27 08:11:22
2015-10-27 23:10:22
2015-10-27 23:10:22
2015-10-27 10:11:22
2015-10-27 06:11:22
2015-10-27 00:11:22
2015-10-27 23:11:22
2015-10-27 00:11:22
2015-10-27 11:11:22
2015-10-27 11:11:22
2015-10-27 12:11:22
2015-10-27 22:11:22
2015-10-27 16:11:22
2015-10-27 14:11:22
2015-10-27 13:11:22
2015-10-27 16:11:22
2015-10-27 23:11:22
2015-10-27 16:11:22
2015-10-27 17:11:22
2015-10-27 18:11:22
2015-10-27 16:11:22
2015-10-27 17:11:22
2015-10-27 18:11:22
2015-10-27 00:11:22
2015-10-27 11:11:22
2015-10-27 12:11:22
2015-10-27 22:11:22
2015-10-27 16:11:22
2015-10-27 14:11:22
2015-10-27 13:11:22
2015-10-27 16:11:22
2015-10-27 17:11:22
2015-10-27 18:11:22
2015-10-27 03:11:22
2015-10-27 20:11:22
2015-10-27 21:11:22
2015-10-27 01:11:22


2.python程序

程序如下:

#-*- coding:utf-8 -*-
__author__ = 'guangliang'

import sys
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

def readFileToMat(filename):
f=open(filename,'r')
matDic={
"00":0,
"01":0,
"02":0,
"03":0,
"04":0,
"05":0,
"06":0,
"07":0,
"08":0,
"09":0,
"10":0,
"11":0,
"12":0,
"13":0,
"14":0,
"15":0,
"16":0,
"17":0,
"18":0,
"19":0,
"20":0,
"21":0,
"22":0,
"23":0
}
hour_arr=[]
num_arr=[]
for line in f.readlines():
hour=line.strip().split(" ")[1].split(":")[0];
if hour in matDic.keys():
matDic[hour]+=1
for key in sorted(matDic.keys()):
hour_arr.append(int(key))
num_arr.append(int(matDic[key]))

return hour_arr,num_arr

def drawPlot(xData,yData):
fig = plt.figure()
ax=fig.add_subplot(111)
#柱状图

ax.bar(xData,yData,facecolor='#9999ff', edgecolor='white', align='center')

#显示数字
for x,y in zip(xData,yData):
ax.text(x, y, y, ha='center', va= 'bottom')

xmajorLocator   = MultipleLocator(1) #将x主刻度标签设置为1的倍数
ax.xaxis.set_major_locator(xmajorLocator)

#设置边界
xmin, xmax = min(xData),max(xData)
ymin, ymax = min(yData),max(yData)

dx=(xmax - xmin) * 0.04
dy=(ymax - ymin) * 0.1
plt.xlim(xmin-dx, xmax+dx)
plt.ylim(ymin-dy, ymax+dy)

#设置坐标轴信息
plt.xtext=plt.xlabel(u'time hour')
plt.ytext=plt.ylabel(u'num')

#出现网格
plt.grid(True)

plt.show()

def main(filename):
hour_arr,num_arr = readFileToMat(filename)
drawPlot(hour_arr,num_arr)

if __name__ == '__main__':
if(len(sys.argv)!=2):
print "Usage:"
print " python didiTimeBar.py filepath/filename"
print "\nFormat:"
print "file content should be like 0000-00-00 00:00:00"
print "for example:"
print " 2015-10-27 06:18:22"
print " 2015-10-27 07:00:27"
print " 2015-10-27 08:11:45"
print " 2015-10-27 09:01:22"
print " 2015-10-27 23:10:02"
print " 2015-10-27 22:51:17"

else:
main(sys.argv[1])


3.结果

执行结果显示如下图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: