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

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.结果

执行结果显示如下图





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值