Python date formatting and plotting

Source: Internet
Author: User

Drawing a curve that changes over time is a requirement that is often encountered, such as a change in the number of software users. The function used to draw a curve that changes over time is matplotlib.pyplot.plot_date (date,num). Because its first variable is of type datetime, a format operation is required first for data entry of type string.

First, using Strptime to format the string 1.1 a basic example

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 Common format-specific correspondence

The common correspondence is shown in the following table (2014-12-28 13:49:30), more on Python for time-date formatting-go to Dawn-Blog Park

; ">year

month

day

hour

minute

second

%y

%y

%m

%b

%d

%h

%i

%m

%s

14

2014

12

Dec

28

13

01

49

30

Second specific drawing examples

Count the number of first-time installs of software in Windows and plot the scatter chart (by day) and histogram (yearly statistics)

Import_winregImportdatetimeImportmatplotlib.datesImportMatplotlib.pyplotImportNumPy 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")PrintKeyall_software=[]k=0Try: I=0 while1: subkey_string=_winreg. EnumKey (key,i)#Print subkey_string        Try: J=0 Temp={} get_installdate=0#Some subkey may not be InstallDate subkey and value             while1:                #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                ifname=="DisplayName":                    #print "Name equals DisplayName"temp["DisplayName"]=valueifname=="InstallDate":                    #print "Name equals InstallDate"temp["InstallDate"]=value Get_installdate=1J+=1exceptWindowserror:#print ' interruptted j= ' +str (j)            ifget_installDate:all_software.append (temp)#print repr (name)        #Print ValueI +=1exceptWindowserror:Print forIteminchRange (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[' -']=0bar_by_year[' -']=0bar_by_year[' the']=0 forIteminchAll_software:ifCount.has_key (item["InstallDate"]): count[item["InstallDate"]]+=1Else: count[item["InstallDate"]]=1Bar_by_year[str (item['InstallDate'].year)]+=1xvalues=[]yvalues=[] forKey,valueinchCount.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, [' -',' -',' the'], rotation='Vertical') matplotlib.pyplot.show ()#value, type = _winreg. QueryValueEx (Key, "Enableautotray")

Iii. reference materials

[1] Python Operations Registry-unharmed-blog Park

[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] sha Hu King | Matplotlib Drawing 2

[6] Python chart drawing: Matplotlib Drawing library Get started-butterfly Meng Zhuang Zhou-Blog Park

[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 date formatting and plotting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.