想用python+matplotlib生成每天用户使用的统计图。测试环境,CentOS6.5,数据存放于mysql中,python2.7,matplotlib使用easy_install安装。
首先建一个表。
CREATE TABLE `member` ( `id` int(1) NOT NULL AUTO_INCREMENT, `user` varchar(15) DEFAULT NULL, `access_date` datetime DEFAULT NULL, `mark` varchar(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
测试数据部分还是写个python脚本生成,这样批量做就方便了。
#!/bin/python #-*- coding:utf-8 -*- # Filename: main.py # Revision: 1.0 # Date: 2012-06-14 # Author: simonzhang # web: www.simonzhang.net # Email: simon-zzm@163.com ### END INIT INFO import time import random # 基础设置 dict = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] # 生成日期的unix起始时间 create_unix_time = 1396281600 # 生成每次日期的间隔时间。单位是小时。 hour_skip_start = 1 hour_skip_end = 12 def main(): start_unix_time = create_unix_time f = open('create.sql', 'wb') # 开始循环生成sql for i in range(280): # 生成用户名 user_name = '' for j in range(random.randint(1,5)): user_name = user_name+random.choice(dict) # 生成时间 start_unix_time = start_unix_time+(random.randint(hour_skip_start, hour_skip_end)*3600) get_create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_unix_time)) f.write("INSERT INTO `member` VALUES ('%s', '%s', '%s', null);\n" % (i, user_name, get_create_time)) f.close() if __name__ == '__main__': main()
开始正式部分,获取数据并生成图片。
#!/bin/python #-*- coding:utf-8 -*- # Filename: main.py # Revision: 1.0 # Date: 2012-06-14 # Author: simonzhang # web: www.simonzhang.net # Email: simon-zzm@163.com ### END INIT INFO import time import MySQLdb from pylab import * from matplotlib.font_manager import FontProperties #### set nearest_day = 32 def create_pic(memberData): print memberData # 将数据x,y整理两个列表 x_date = [] y_count = [] for i in memberData: x_date.append(i[2]) y_count.append(i[1]) # 将x轴日期元组化 zu = [] da = [] for j in range(1, len(x_date)/5+1): zu.append(j*5) da.append(x_date[j*5]) # 开始生成表 font = FontProperties(fname=r"/usr/share/fonts/truetype/freefont/msyh.ttf", size=10) cla() label = u'测试' plot(y_count) # x和y轴定义 xlabel(u'日期', fontproperties=font) ylabel(u'统计数量', fontproperties=font) xticks(tuple(zu), tuple(da), fontproperties=font) title(u'统计', fontproperties=font) grid(True) legend(loc = 'lower right') savefig("test1.png") def main(): try: conn=MySQLdb.connect(host='115.28.42.253',user='test',passwd='123456',db='pymatplotlib',port=3306,charset='utf8') cur=conn.cursor() start_date = time.strftime('%Y-%m-%d', time.localtime(time.time()-3600*24*nearest_day)) end_date = time.strftime('%Y-%m-%d', time.localtime(time.time())) my_sql = "SELECT id,count(*),DATE_FORMAT(access_date, '%Y-%m-%d' ) \ FROM member \ WHERE access_date BETWEEN '"+start_date+"' and '"+end_date+"' \ GROUP BY DATE_FORMAT(access_date, '%Y-%m-%d' ) " print my_sql cur.execute(my_sql) cds=cur.fetchall() conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) create_pic(cds) if __name__ == '__main__': main()
发表评论