国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

python日期格式化與繪圖

2019-11-14 17:26:10
字體:
供稿:網(wǎng)友

   畫一個量隨著時間變化的曲線是經(jīng)常會遇到的需求,比如畫軟件用戶數(shù)的變化曲線。畫隨時間變化的曲線主要用到的函數(shù)是matplotlib.pyplot.plot_date(date,num)。由于其第一個變量是datetime類型的,所以對于string類型的數(shù)據(jù)輸入,首先需要進(jìn)行格式化操作。

一、使用strptime進(jìn)行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常見的具體格式化對應(yīng)關(guān)系

   常見的對應(yīng)關(guān)系如下表所示(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

二、一個具體的畫圖例子

統(tǒng)計windows下軟件首次安裝的數(shù)目并畫出散點圖(按日統(tǒng)計)和柱狀圖(按年統(tǒng)計)

import _winregimport datetimeimport matplotlib.datesimport matplotlib.pyplot import numpy as npprint "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 keyall_software=[]k=0try:    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 +=1except WindowsError:    printfor 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['2012']=0bar_by_year['2013']=0bar_by_year['2014']=0for 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)]+=1xvalues=[]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, ['2012','2013','2014'], rotation='vertical')matplotlib.pyplot.show()#value, type = _winreg.QueryValueEx(key, "EnableAutoTray")

 

 

三、參考材料

[1]Python 操作注冊表 - 無恙 - 博客園

[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

[5]沙湖王 | matplotlib繪圖2

[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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 万源市| 南投县| 家居| 石楼县| 大埔县| 哈巴河县| 禹城市| 三穗县| 海阳市| 陵川县| 溧阳市| 宜川县| 道孚县| 收藏| 凌源市| 鄂托克旗| 南汇区| 泸西县| 波密县| 武冈市| 罗山县| 闸北区| 出国| 利辛县| 兴海县| 江陵县| 宝清县| 兰溪市| 都江堰市| 磐石市| 清水河县| 方山县| 新昌县| 双城市| 广宗县| 五原县| 阳泉市| 秭归县| 莒南县| 福州市| 西峡县|