Matplotlib以小时为单位统计文件数据
来源:互联网 发布:淘宝商城女童装 编辑:程序博客网 时间:2024/05/01 01:45
Matplotlib 统计数据
统计文件中以小时hour为单位的数据数量
1.文件
文件内容如下:
2015-10-27 09:11:222015-10-27 00:11:222015-10-27 15:11:222015-10-27 02:11:222015-10-27 19:11:222015-10-27 04:11:222015-10-27 05:11:222015-10-27 06:11:222015-10-27 07:11:222015-10-27 09:11:222015-10-27 01:11:222015-10-27 01:11:222015-10-27 01:11:222015-10-27 08:11:222015-10-27 23:10:222015-10-27 23:10:222015-10-27 10:11:222015-10-27 06:11:222015-10-27 00:11:222015-10-27 23:11:222015-10-27 00:11:222015-10-27 11:11:222015-10-27 11:11:222015-10-27 12:11:222015-10-27 22:11:222015-10-27 16:11:222015-10-27 14:11:222015-10-27 13:11:222015-10-27 16:11:222015-10-27 23:11:222015-10-27 16:11:222015-10-27 17:11:222015-10-27 18:11:222015-10-27 16:11:222015-10-27 17:11:222015-10-27 18:11:222015-10-27 00:11:222015-10-27 11:11:222015-10-27 12:11:222015-10-27 22:11:222015-10-27 16:11:222015-10-27 14:11:222015-10-27 13:11:222015-10-27 16:11:222015-10-27 17:11:222015-10-27 18:11:222015-10-27 03:11:222015-10-27 20:11:222015-10-27 21:11:222015-10-27 01:11:22
2.python程序
程序如下:#-*- coding:utf-8 -*-__author__ = 'guangliang'import sysimport matplotlib.pyplot as pltfrom matplotlib.ticker import MultipleLocatordef 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_arrdef 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.结果
执行结果显示如下图
0 0
- Matplotlib以小时为单位统计文件数据
- 网站访问统计,以小时为单位
- 日期时间以天为单位统计小时区间时间段数据
- access 通过union以天为单位统计当天的每个时段的数据(24H)
- SQLserver统计某个字段不重复的个数以及怎么以时间为单位取出一段时间的数据
- [MapReduce]找出每个用户最喜欢po贴的时间(以小时为单位)
- 以字节为单位显示变量的内存存储数据
- 以字节为单位显示变量的内存存储数据
- 以字节为单位显示变量的内存存储数据
- java 以字节为单位读取文件(2)
- java 以字符为单位读取文件(3)
- 以文件为单位的分句、分词python封装脚本
- java 实现以字节为单位读取文件(2)
- java 实现以字符为单位读取文件(3)
- 以/为单位截取URL
- ls显示文件大小时显示单位
- 以行为单位读写文件
- 读写文件1——以字节为单位,读写文件(笔记实例)
- 基因检测-外显子链接问题
- 第9周—项目3(2)两个稀疏矩阵相加的运算
- TCP长连接与短连接的区别
- SVM系列
- 003.main 主函数文件
- Matplotlib以小时为单位统计文件数据
- 怎样编写概要设计
- Mybatis批量插入、更新
- ListView局部更新
- 编写测量矩阵代码需要用到的几个常见函数
- 堆排序(C#)
- 基因检测-基因系统聚类
- 002.FKPerson 类的实现文件
- 遇到 scp报错:not a regular file 解决 so easy!!!