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.结果
执行结果显示如下图相关文章推荐
- Thinkphp——商品加入购物车在购物车列表显示(还有购物车删除)
- php开发日记之生成图片
- 详解PHP序列化反序列化的方法
- 工厂方法及其设计原则
- 使用 OpCache 提升 PHP 性能
- php使用ZipArchive函数实现文件的压缩与解压缩
- 使用PHP搭建自己的MVC框架
- 学习PHP到底要学习哪些东西?
- php过滤所有的空白字符(空格、全角空格、换行等)
- 005.Output 格式符
- webstorm与phpstorm主题配置
- PHP的APC模块实现上传进度条
- php yii .htaccess
- PHP将图片处理成圆角
- 将数据库转php json数组格式
- php做购物商品库存解决方法
- 第 四 十 二 天:FTP 的 相 关 问 题
- php+ajax制作无刷新留言板
- php处理字符串格式的计算公式
- 一个完整的php文件上传类实例讲解