python日期格式化与绘图
画一个量随着时间变化的曲线是经常会遇到的需求,比如画软件用户数的变化曲线。画随时间变化的曲线主要用到的函数是matplotlib.pyplot.plot_date(date,num)。由于其第一个变量是datetime类型的,所以对于string类型的数据输入,首先需要进行格式化操作。
一、使用strptime进行string的格式化
1.1一个基本例子
time=datetime.datetime.strptime('2014-12-28 13:49:30','%Y-%m-%d %H:%M:%S')
print time
2014-12-28 13:49:30
1.2常见的具体格式化对应关系
常见的对应关系如下表所示(2014-12-28 13:49:30),更多可以参见python对时间日期做格式化 - 走到天亮 - 博客园
Year |
Month |
Day |
Hour |
Minute |
Second |
|||
%y |
%Y |
%m |
%b |
%d |
%H |
%I |
%M |
%S |
14 |
2014 |
12 |
Dec |
28 |
13 |
01 |
49 |
30 |
二、一个具体的画图例子
统计windows下软件首次安装的数目并画出散点图(按日统计)和柱状图(按年统计)
import _winreg
import datetime
import matplotlib.dates
import matplotlib.pyplot
import numpy as np
print "import winreg done!" key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
#key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")
print key
all_software=[]
k=0
try:
i=0
while 1:
subkey_string=_winreg.EnumKey(key,i)
#print subkey_string
try:
j=0
temp={}
get_installDate=0
#some subkey may not have installdate subkey and value
while 1:
#subkey=_winreg.OpenKey(key,"InstallDate")
subkey=_winreg.OpenKey(key,subkey_string)
name, value, type_value = _winreg.EnumValue(subkey,j)
#print repr(name),repr(value)
#print "name is "+name
if name=="DisplayName":
#print "Name equals DisplayName"
temp["DisplayName"]=value
if name=="InstallDate":
#print "Name equals installdate"
temp["InstallDate"]=value
get_installDate=1
j+=1
except WindowsError:
#print 'interruptted j='+str(j)
if get_installDate:
all_software.append(temp)
#print repr(name)
#print value
i +=1
except WindowsError:
for item in range(len(all_software)):
all_software[item]["InstallDate"]=datetime.datetime.strptime(all_software[item]["InstallDate"].encode('ascii','ignore'),'%Y%m%d')
count={}
bar_by_year={}
bar_by_year['']=0
bar_by_year['']=0
bar_by_year['']=0
for item in all_software:
if count.has_key(item["InstallDate"]):
count[item["InstallDate"]]+=1
else:
count[item["InstallDate"]]=1
bar_by_year[str(item['InstallDate'].year)]+=1
xvalues=[]
yvalues=[]
for key,value in count.iteritems():
xvalues.append(key)
yvalues.append(value)
xvalues=matplotlib.dates.date2num(xvalues)
matplotlib.pyplot.figure(1)
matplotlib.pyplot.plot_date(xvalues,yvalues)
matplotlib.pyplot.figure(2)
xvalues_2=np.arange(3)
yvalues_2=bar_by_year.values()
fig2=matplotlib.pyplot.bar(xvalues_2,yvalues_2,0.5)
matplotlib.pyplot.xticks(xvalues_2+0.25, ['','',''], rotation='vertical')
matplotlib.pyplot.show() #value, type = _winreg.QueryValueEx(key, "EnableAutoTray")
三、参考材料
[2]c++ - How can I enumerate/list all installed applications in Windows XP? - Stack Overflow
[3]winreg – Windows registry access — Python v3.0.1 documentation
[4]graph - plotting time in python with matplotlib - Stack Overflow
[6]Python图表绘制:matplotlib绘图库入门 - 蝶梦庄周 - 博客园
[7]ticks_and_spines example code: ticklabels_demo_rotation.py — Matplotlib 1.4.2 documentation
[8]api example code: barchart_demo.py — Matplotlib 1.4.2 documentation
python日期格式化与绘图的更多相关文章
- Python日期格式化知识
Python中日期格式化是非常常见的操作,Python 中能用很多方式处理日期和时间,转换日期格式是一个常见的功能.Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时 ...
- python日期格式化符号
python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数( ...
- python 日期格式化常用标记
符号 说明 例子 %a 英文星期的简写 Mon %A 英文星期的完整编写 Monday %b 英文月份的简写 Jun %B 英文月份的完整编写 June ...
- Python 日期格式化 及 schwartzian排序
__author__ = 'root' import datetime import time import copy # 12/Dec/2012:23:59:50 # 12/Sep/2012:23: ...
- python中时间日期格式化符号
python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...
- python time模块介绍(日期格式化 时间戳)
import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...
- Python 日期和时间_python 当前日期时间_python日期格式化
Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...
- 1、Python 日期时间格式化输出
今天帮朋友写自动化脚本,又需要用格式化日期,又忘记怎么写了,还是写到自己博客里面,方便日后需要的时候看一眼吧.So,临时加一篇 Python 的文章. 1.Python的time模块 import t ...
- python 日期生成和时间格式化
记录下日期时间的转换和生成:1.这个是使用datetime 函数生成往后几天的时间,比如当前日期是2019-07-01 那么控制days=1然后再和当前的时间相加,就能得到明天的日期def time_ ...
随机推荐
- Ubuntu 16.04 LTS 安装配置 Nginx 1.10.0 Php7.0-FPM
1. 安装Nginx,Php-7.0 ~$ sudo add-apt-repository ppa:nginx/stable ~$ sudo apt-get update ~$ sudo apt-ge ...
- CP强制覆盖
发现在Fedora 10 /ubutun 里面用cp -fr src dest,即使加了-f也是不能强行覆盖的,这时怎么回事的呢?一两个文件还好说,就输几个yes吧,但是要是n多文件怎么办,那还不输死 ...
- iOS指南针
前言: 这个小项目使用到了CoreLocation框架里面的设备朝向功能,对CoreLocation感兴趣的可以翻一下之前的文章 在另一个博客站有朋友发现一个尴尬的问题(图片的东西2个方向是不对的), ...
- Android微信登陆
前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.co ...
- Android源码分析之SharedPreferences
在Android的日常开发中,相信大家都用过SharedPreferences来保存用户的某些settings值.Shared Preferences 以键值对的形式存储私有的原生类型数据,这里的私有 ...
- 理解并自定义HttpHandler
前言 之前从网上找了几篇讲解如何自定义HttpHandler的文章,依葫芦画瓢却一直没成功过.经过上一篇<asp.net管道模型(管线模型)之一发不可收拾>的总结,对管道模型和请求/响应过 ...
- C#八皇后问题 枚举值
记得刚出道的时候, 有考虑怎么面试, 以及可能会遇到的面试题, 有一个人说了一下 八皇后问题, 据说要用 sql 语句写出来, 暂时我 写了一个C#版本的, 经测验,八皇后算法结果为 92种, 这个与 ...
- Sql Server 2008R2 遇到了BCP导入各种中文乱码的问题
今天玩BCP导入数据的时候,有文件格式,有中文字符串问题……以下是历程,和大家分享一下,希望不要走我的弯路 主要那个表是一个翻译表,一个文件里面内涵几十种语言,所以很容易发现问题. 0.使用最常用的语 ...
- sql server 2008 相关基础(物理备份还原)
1. restore database KLJY_juncsoft from disk= 'D:\\kljy_2012_12_28.bak' with recovery 注意在还原时,进度一直显示0% ...
- 使用国内 maven 镜像 代替国外 mirror
使用maven的都知道国外的maven下载一个是比较慢,一个是因为被墙,一些jar包无法下载,非常老火. 比如出现类似下面的错误: Unknown host repo.maven.apache.org ...